cpm1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * General Purpose functions for the global management of the
  4. * Communication Processor Module.
  5. * Copyright (c) 1997 Dan error_act (dmalek@jlc.net)
  6. *
  7. * In addition to the individual control of the communication
  8. * channels, there are a few functions that globally affect the
  9. * communication processor.
  10. *
  11. * Buffer descriptors must be allocated from the dual ported memory
  12. * space. The allocator for that is here. When the communication
  13. * process is reset, we reclaim the memory available. There is
  14. * currently no deallocator for this memory.
  15. * The amount of space available is platform dependent. On the
  16. * MBX, the EPPC software loads additional microcode into the
  17. * communication processor, and uses some of the DP ram for this
  18. * purpose. Current, the first 512 bytes and the last 256 bytes of
  19. * memory are used. Right now I am conservative and only use the
  20. * memory that can never be used for microcode. If there are
  21. * applications that require more DP ram, we can expand the boundaries
  22. * but then we have to be careful of any downloaded microcode.
  23. */
  24. #include <linux/errno.h>
  25. #include <linux/sched.h>
  26. #include <linux/kernel.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/param.h>
  29. #include <linux/string.h>
  30. #include <linux/mm.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/irq.h>
  33. #include <linux/module.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/slab.h>
  36. #include <asm/page.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/8xx_immap.h>
  39. #include <asm/cpm1.h>
  40. #include <asm/io.h>
  41. #include <asm/tlbflush.h>
  42. #include <asm/rheap.h>
  43. #include <asm/prom.h>
  44. #include <asm/cpm.h>
  45. #include <asm/fs_pd.h>
  46. #ifdef CONFIG_8xx_GPIO
  47. #include <linux/of_gpio.h>
  48. #endif
  49. #define CPM_MAP_SIZE (0x4000)
  50. cpm8xx_t __iomem *cpmp; /* Pointer to comm processor space */
  51. immap_t __iomem *mpc8xx_immr;
  52. static cpic8xx_t __iomem *cpic_reg;
  53. static struct irq_domain *cpm_pic_host;
  54. static void cpm_mask_irq(struct irq_data *d)
  55. {
  56. unsigned int cpm_vec = (unsigned int)irqd_to_hwirq(d);
  57. clrbits32(&cpic_reg->cpic_cimr, (1 << cpm_vec));
  58. }
  59. static void cpm_unmask_irq(struct irq_data *d)
  60. {
  61. unsigned int cpm_vec = (unsigned int)irqd_to_hwirq(d);
  62. setbits32(&cpic_reg->cpic_cimr, (1 << cpm_vec));
  63. }
  64. static void cpm_end_irq(struct irq_data *d)
  65. {
  66. unsigned int cpm_vec = (unsigned int)irqd_to_hwirq(d);
  67. out_be32(&cpic_reg->cpic_cisr, (1 << cpm_vec));
  68. }
  69. static struct irq_chip cpm_pic = {
  70. .name = "CPM PIC",
  71. .irq_mask = cpm_mask_irq,
  72. .irq_unmask = cpm_unmask_irq,
  73. .irq_eoi = cpm_end_irq,
  74. };
  75. int cpm_get_irq(void)
  76. {
  77. int cpm_vec;
  78. /* Get the vector by setting the ACK bit and then reading
  79. * the register.
  80. */
  81. out_be16(&cpic_reg->cpic_civr, 1);
  82. cpm_vec = in_be16(&cpic_reg->cpic_civr);
  83. cpm_vec >>= 11;
  84. return irq_linear_revmap(cpm_pic_host, cpm_vec);
  85. }
  86. static int cpm_pic_host_map(struct irq_domain *h, unsigned int virq,
  87. irq_hw_number_t hw)
  88. {
  89. pr_debug("cpm_pic_host_map(%d, 0x%lx)\n", virq, hw);
  90. irq_set_status_flags(virq, IRQ_LEVEL);
  91. irq_set_chip_and_handler(virq, &cpm_pic, handle_fasteoi_irq);
  92. return 0;
  93. }
  94. /* The CPM can generate the error interrupt when there is a race condition
  95. * between generating and masking interrupts. All we have to do is ACK it
  96. * and return. This is a no-op function so we don't need any special
  97. * tests in the interrupt handler.
  98. */
  99. static irqreturn_t cpm_error_interrupt(int irq, void *dev)
  100. {
  101. return IRQ_HANDLED;
  102. }
  103. static struct irqaction cpm_error_irqaction = {
  104. .handler = cpm_error_interrupt,
  105. .flags = IRQF_NO_THREAD,
  106. .name = "error",
  107. };
  108. static const struct irq_domain_ops cpm_pic_host_ops = {
  109. .map = cpm_pic_host_map,
  110. };
  111. unsigned int cpm_pic_init(void)
  112. {
  113. struct device_node *np = NULL;
  114. struct resource res;
  115. unsigned int sirq = 0, hwirq, eirq;
  116. int ret;
  117. pr_debug("cpm_pic_init\n");
  118. np = of_find_compatible_node(NULL, NULL, "fsl,cpm1-pic");
  119. if (np == NULL)
  120. np = of_find_compatible_node(NULL, "cpm-pic", "CPM");
  121. if (np == NULL) {
  122. printk(KERN_ERR "CPM PIC init: can not find cpm-pic node\n");
  123. return sirq;
  124. }
  125. ret = of_address_to_resource(np, 0, &res);
  126. if (ret)
  127. goto end;
  128. cpic_reg = ioremap(res.start, resource_size(&res));
  129. if (cpic_reg == NULL)
  130. goto end;
  131. sirq = irq_of_parse_and_map(np, 0);
  132. if (!sirq)
  133. goto end;
  134. /* Initialize the CPM interrupt controller. */
  135. hwirq = (unsigned int)virq_to_hw(sirq);
  136. out_be32(&cpic_reg->cpic_cicr,
  137. (CICR_SCD_SCC4 | CICR_SCC_SCC3 | CICR_SCB_SCC2 | CICR_SCA_SCC1) |
  138. ((hwirq/2) << 13) | CICR_HP_MASK);
  139. out_be32(&cpic_reg->cpic_cimr, 0);
  140. cpm_pic_host = irq_domain_add_linear(np, 64, &cpm_pic_host_ops, NULL);
  141. if (cpm_pic_host == NULL) {
  142. printk(KERN_ERR "CPM2 PIC: failed to allocate irq host!\n");
  143. sirq = 0;
  144. goto end;
  145. }
  146. /* Install our own error handler. */
  147. np = of_find_compatible_node(NULL, NULL, "fsl,cpm1");
  148. if (np == NULL)
  149. np = of_find_node_by_type(NULL, "cpm");
  150. if (np == NULL) {
  151. printk(KERN_ERR "CPM PIC init: can not find cpm node\n");
  152. goto end;
  153. }
  154. eirq = irq_of_parse_and_map(np, 0);
  155. if (!eirq)
  156. goto end;
  157. if (setup_irq(eirq, &cpm_error_irqaction))
  158. printk(KERN_ERR "Could not allocate CPM error IRQ!");
  159. setbits32(&cpic_reg->cpic_cicr, CICR_IEN);
  160. end:
  161. of_node_put(np);
  162. return sirq;
  163. }
  164. void __init cpm_reset(void)
  165. {
  166. sysconf8xx_t __iomem *siu_conf;
  167. mpc8xx_immr = ioremap(get_immrbase(), 0x4000);
  168. if (!mpc8xx_immr) {
  169. printk(KERN_CRIT "Could not map IMMR\n");
  170. return;
  171. }
  172. cpmp = &mpc8xx_immr->im_cpm;
  173. #ifndef CONFIG_PPC_EARLY_DEBUG_CPM
  174. /* Perform a reset.
  175. */
  176. out_be16(&cpmp->cp_cpcr, CPM_CR_RST | CPM_CR_FLG);
  177. /* Wait for it.
  178. */
  179. while (in_be16(&cpmp->cp_cpcr) & CPM_CR_FLG);
  180. #endif
  181. #ifdef CONFIG_UCODE_PATCH
  182. cpm_load_patch(cpmp);
  183. #endif
  184. /* Set SDMA Bus Request priority 5.
  185. * On 860T, this also enables FEC priority 6. I am not sure
  186. * this is what we really want for some applications, but the
  187. * manual recommends it.
  188. * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
  189. */
  190. siu_conf = immr_map(im_siu_conf);
  191. if ((mfspr(SPRN_IMMR) & 0xffff) == 0x0900) /* MPC885 */
  192. out_be32(&siu_conf->sc_sdcr, 0x40);
  193. else
  194. out_be32(&siu_conf->sc_sdcr, 1);
  195. immr_unmap(siu_conf);
  196. }
  197. static DEFINE_SPINLOCK(cmd_lock);
  198. #define MAX_CR_CMD_LOOPS 10000
  199. int cpm_command(u32 command, u8 opcode)
  200. {
  201. int i, ret;
  202. unsigned long flags;
  203. if (command & 0xffffff0f)
  204. return -EINVAL;
  205. spin_lock_irqsave(&cmd_lock, flags);
  206. ret = 0;
  207. out_be16(&cpmp->cp_cpcr, command | CPM_CR_FLG | (opcode << 8));
  208. for (i = 0; i < MAX_CR_CMD_LOOPS; i++)
  209. if ((in_be16(&cpmp->cp_cpcr) & CPM_CR_FLG) == 0)
  210. goto out;
  211. printk(KERN_ERR "%s(): Not able to issue CPM command\n", __func__);
  212. ret = -EIO;
  213. out:
  214. spin_unlock_irqrestore(&cmd_lock, flags);
  215. return ret;
  216. }
  217. EXPORT_SYMBOL(cpm_command);
  218. /* Set a baud rate generator. This needs lots of work. There are
  219. * four BRGs, any of which can be wired to any channel.
  220. * The internal baud rate clock is the system clock divided by 16.
  221. * This assumes the baudrate is 16x oversampled by the uart.
  222. */
  223. #define BRG_INT_CLK (get_brgfreq())
  224. #define BRG_UART_CLK (BRG_INT_CLK/16)
  225. #define BRG_UART_CLK_DIV16 (BRG_UART_CLK/16)
  226. void
  227. cpm_setbrg(uint brg, uint rate)
  228. {
  229. u32 __iomem *bp;
  230. /* This is good enough to get SMCs running.....
  231. */
  232. bp = &cpmp->cp_brgc1;
  233. bp += brg;
  234. /* The BRG has a 12-bit counter. For really slow baud rates (or
  235. * really fast processors), we may have to further divide by 16.
  236. */
  237. if (((BRG_UART_CLK / rate) - 1) < 4096)
  238. out_be32(bp, (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN);
  239. else
  240. out_be32(bp, (((BRG_UART_CLK_DIV16 / rate) - 1) << 1) |
  241. CPM_BRG_EN | CPM_BRG_DIV16);
  242. }
  243. struct cpm_ioport16 {
  244. __be16 dir, par, odr_sor, dat, intr;
  245. __be16 res[3];
  246. };
  247. struct cpm_ioport32b {
  248. __be32 dir, par, odr, dat;
  249. };
  250. struct cpm_ioport32e {
  251. __be32 dir, par, sor, odr, dat;
  252. };
  253. static void cpm1_set_pin32(int port, int pin, int flags)
  254. {
  255. struct cpm_ioport32e __iomem *iop;
  256. pin = 1 << (31 - pin);
  257. if (port == CPM_PORTB)
  258. iop = (struct cpm_ioport32e __iomem *)
  259. &mpc8xx_immr->im_cpm.cp_pbdir;
  260. else
  261. iop = (struct cpm_ioport32e __iomem *)
  262. &mpc8xx_immr->im_cpm.cp_pedir;
  263. if (flags & CPM_PIN_OUTPUT)
  264. setbits32(&iop->dir, pin);
  265. else
  266. clrbits32(&iop->dir, pin);
  267. if (!(flags & CPM_PIN_GPIO))
  268. setbits32(&iop->par, pin);
  269. else
  270. clrbits32(&iop->par, pin);
  271. if (port == CPM_PORTB) {
  272. if (flags & CPM_PIN_OPENDRAIN)
  273. setbits16(&mpc8xx_immr->im_cpm.cp_pbodr, pin);
  274. else
  275. clrbits16(&mpc8xx_immr->im_cpm.cp_pbodr, pin);
  276. }
  277. if (port == CPM_PORTE) {
  278. if (flags & CPM_PIN_SECONDARY)
  279. setbits32(&iop->sor, pin);
  280. else
  281. clrbits32(&iop->sor, pin);
  282. if (flags & CPM_PIN_OPENDRAIN)
  283. setbits32(&mpc8xx_immr->im_cpm.cp_peodr, pin);
  284. else
  285. clrbits32(&mpc8xx_immr->im_cpm.cp_peodr, pin);
  286. }
  287. }
  288. static void cpm1_set_pin16(int port, int pin, int flags)
  289. {
  290. struct cpm_ioport16 __iomem *iop =
  291. (struct cpm_ioport16 __iomem *)&mpc8xx_immr->im_ioport;
  292. pin = 1 << (15 - pin);
  293. if (port != 0)
  294. iop += port - 1;
  295. if (flags & CPM_PIN_OUTPUT)
  296. setbits16(&iop->dir, pin);
  297. else
  298. clrbits16(&iop->dir, pin);
  299. if (!(flags & CPM_PIN_GPIO))
  300. setbits16(&iop->par, pin);
  301. else
  302. clrbits16(&iop->par, pin);
  303. if (port == CPM_PORTA) {
  304. if (flags & CPM_PIN_OPENDRAIN)
  305. setbits16(&iop->odr_sor, pin);
  306. else
  307. clrbits16(&iop->odr_sor, pin);
  308. }
  309. if (port == CPM_PORTC) {
  310. if (flags & CPM_PIN_SECONDARY)
  311. setbits16(&iop->odr_sor, pin);
  312. else
  313. clrbits16(&iop->odr_sor, pin);
  314. if (flags & CPM_PIN_FALLEDGE)
  315. setbits16(&iop->intr, pin);
  316. else
  317. clrbits16(&iop->intr, pin);
  318. }
  319. }
  320. void cpm1_set_pin(enum cpm_port port, int pin, int flags)
  321. {
  322. if (port == CPM_PORTB || port == CPM_PORTE)
  323. cpm1_set_pin32(port, pin, flags);
  324. else
  325. cpm1_set_pin16(port, pin, flags);
  326. }
  327. int cpm1_clk_setup(enum cpm_clk_target target, int clock, int mode)
  328. {
  329. int shift;
  330. int i, bits = 0;
  331. u32 __iomem *reg;
  332. u32 mask = 7;
  333. u8 clk_map[][3] = {
  334. {CPM_CLK_SCC1, CPM_BRG1, 0},
  335. {CPM_CLK_SCC1, CPM_BRG2, 1},
  336. {CPM_CLK_SCC1, CPM_BRG3, 2},
  337. {CPM_CLK_SCC1, CPM_BRG4, 3},
  338. {CPM_CLK_SCC1, CPM_CLK1, 4},
  339. {CPM_CLK_SCC1, CPM_CLK2, 5},
  340. {CPM_CLK_SCC1, CPM_CLK3, 6},
  341. {CPM_CLK_SCC1, CPM_CLK4, 7},
  342. {CPM_CLK_SCC2, CPM_BRG1, 0},
  343. {CPM_CLK_SCC2, CPM_BRG2, 1},
  344. {CPM_CLK_SCC2, CPM_BRG3, 2},
  345. {CPM_CLK_SCC2, CPM_BRG4, 3},
  346. {CPM_CLK_SCC2, CPM_CLK1, 4},
  347. {CPM_CLK_SCC2, CPM_CLK2, 5},
  348. {CPM_CLK_SCC2, CPM_CLK3, 6},
  349. {CPM_CLK_SCC2, CPM_CLK4, 7},
  350. {CPM_CLK_SCC3, CPM_BRG1, 0},
  351. {CPM_CLK_SCC3, CPM_BRG2, 1},
  352. {CPM_CLK_SCC3, CPM_BRG3, 2},
  353. {CPM_CLK_SCC3, CPM_BRG4, 3},
  354. {CPM_CLK_SCC3, CPM_CLK5, 4},
  355. {CPM_CLK_SCC3, CPM_CLK6, 5},
  356. {CPM_CLK_SCC3, CPM_CLK7, 6},
  357. {CPM_CLK_SCC3, CPM_CLK8, 7},
  358. {CPM_CLK_SCC4, CPM_BRG1, 0},
  359. {CPM_CLK_SCC4, CPM_BRG2, 1},
  360. {CPM_CLK_SCC4, CPM_BRG3, 2},
  361. {CPM_CLK_SCC4, CPM_BRG4, 3},
  362. {CPM_CLK_SCC4, CPM_CLK5, 4},
  363. {CPM_CLK_SCC4, CPM_CLK6, 5},
  364. {CPM_CLK_SCC4, CPM_CLK7, 6},
  365. {CPM_CLK_SCC4, CPM_CLK8, 7},
  366. {CPM_CLK_SMC1, CPM_BRG1, 0},
  367. {CPM_CLK_SMC1, CPM_BRG2, 1},
  368. {CPM_CLK_SMC1, CPM_BRG3, 2},
  369. {CPM_CLK_SMC1, CPM_BRG4, 3},
  370. {CPM_CLK_SMC1, CPM_CLK1, 4},
  371. {CPM_CLK_SMC1, CPM_CLK2, 5},
  372. {CPM_CLK_SMC1, CPM_CLK3, 6},
  373. {CPM_CLK_SMC1, CPM_CLK4, 7},
  374. {CPM_CLK_SMC2, CPM_BRG1, 0},
  375. {CPM_CLK_SMC2, CPM_BRG2, 1},
  376. {CPM_CLK_SMC2, CPM_BRG3, 2},
  377. {CPM_CLK_SMC2, CPM_BRG4, 3},
  378. {CPM_CLK_SMC2, CPM_CLK5, 4},
  379. {CPM_CLK_SMC2, CPM_CLK6, 5},
  380. {CPM_CLK_SMC2, CPM_CLK7, 6},
  381. {CPM_CLK_SMC2, CPM_CLK8, 7},
  382. };
  383. switch (target) {
  384. case CPM_CLK_SCC1:
  385. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  386. shift = 0;
  387. break;
  388. case CPM_CLK_SCC2:
  389. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  390. shift = 8;
  391. break;
  392. case CPM_CLK_SCC3:
  393. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  394. shift = 16;
  395. break;
  396. case CPM_CLK_SCC4:
  397. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  398. shift = 24;
  399. break;
  400. case CPM_CLK_SMC1:
  401. reg = &mpc8xx_immr->im_cpm.cp_simode;
  402. shift = 12;
  403. break;
  404. case CPM_CLK_SMC2:
  405. reg = &mpc8xx_immr->im_cpm.cp_simode;
  406. shift = 28;
  407. break;
  408. default:
  409. printk(KERN_ERR "cpm1_clock_setup: invalid clock target\n");
  410. return -EINVAL;
  411. }
  412. for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
  413. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  414. bits = clk_map[i][2];
  415. break;
  416. }
  417. }
  418. if (i == ARRAY_SIZE(clk_map)) {
  419. printk(KERN_ERR "cpm1_clock_setup: invalid clock combination\n");
  420. return -EINVAL;
  421. }
  422. bits <<= shift;
  423. mask <<= shift;
  424. if (reg == &mpc8xx_immr->im_cpm.cp_sicr) {
  425. if (mode == CPM_CLK_RTX) {
  426. bits |= bits << 3;
  427. mask |= mask << 3;
  428. } else if (mode == CPM_CLK_RX) {
  429. bits <<= 3;
  430. mask <<= 3;
  431. }
  432. }
  433. out_be32(reg, (in_be32(reg) & ~mask) | bits);
  434. return 0;
  435. }
  436. /*
  437. * GPIO LIB API implementation
  438. */
  439. #ifdef CONFIG_8xx_GPIO
  440. struct cpm1_gpio16_chip {
  441. struct of_mm_gpio_chip mm_gc;
  442. spinlock_t lock;
  443. /* shadowed data register to clear/set bits safely */
  444. u16 cpdata;
  445. /* IRQ associated with Pins when relevant */
  446. int irq[16];
  447. };
  448. static void cpm1_gpio16_save_regs(struct of_mm_gpio_chip *mm_gc)
  449. {
  450. struct cpm1_gpio16_chip *cpm1_gc =
  451. container_of(mm_gc, struct cpm1_gpio16_chip, mm_gc);
  452. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  453. cpm1_gc->cpdata = in_be16(&iop->dat);
  454. }
  455. static int cpm1_gpio16_get(struct gpio_chip *gc, unsigned int gpio)
  456. {
  457. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  458. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  459. u16 pin_mask;
  460. pin_mask = 1 << (15 - gpio);
  461. return !!(in_be16(&iop->dat) & pin_mask);
  462. }
  463. static void __cpm1_gpio16_set(struct of_mm_gpio_chip *mm_gc, u16 pin_mask,
  464. int value)
  465. {
  466. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  467. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  468. if (value)
  469. cpm1_gc->cpdata |= pin_mask;
  470. else
  471. cpm1_gc->cpdata &= ~pin_mask;
  472. out_be16(&iop->dat, cpm1_gc->cpdata);
  473. }
  474. static void cpm1_gpio16_set(struct gpio_chip *gc, unsigned int gpio, int value)
  475. {
  476. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  477. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  478. unsigned long flags;
  479. u16 pin_mask = 1 << (15 - gpio);
  480. spin_lock_irqsave(&cpm1_gc->lock, flags);
  481. __cpm1_gpio16_set(mm_gc, pin_mask, value);
  482. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  483. }
  484. static int cpm1_gpio16_to_irq(struct gpio_chip *gc, unsigned int gpio)
  485. {
  486. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  487. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  488. return cpm1_gc->irq[gpio] ? : -ENXIO;
  489. }
  490. static int cpm1_gpio16_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  491. {
  492. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  493. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  494. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  495. unsigned long flags;
  496. u16 pin_mask = 1 << (15 - gpio);
  497. spin_lock_irqsave(&cpm1_gc->lock, flags);
  498. setbits16(&iop->dir, pin_mask);
  499. __cpm1_gpio16_set(mm_gc, pin_mask, val);
  500. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  501. return 0;
  502. }
  503. static int cpm1_gpio16_dir_in(struct gpio_chip *gc, unsigned int gpio)
  504. {
  505. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  506. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  507. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  508. unsigned long flags;
  509. u16 pin_mask = 1 << (15 - gpio);
  510. spin_lock_irqsave(&cpm1_gc->lock, flags);
  511. clrbits16(&iop->dir, pin_mask);
  512. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  513. return 0;
  514. }
  515. int cpm1_gpiochip_add16(struct device *dev)
  516. {
  517. struct device_node *np = dev->of_node;
  518. struct cpm1_gpio16_chip *cpm1_gc;
  519. struct of_mm_gpio_chip *mm_gc;
  520. struct gpio_chip *gc;
  521. u16 mask;
  522. cpm1_gc = kzalloc(sizeof(*cpm1_gc), GFP_KERNEL);
  523. if (!cpm1_gc)
  524. return -ENOMEM;
  525. spin_lock_init(&cpm1_gc->lock);
  526. if (!of_property_read_u16(np, "fsl,cpm1-gpio-irq-mask", &mask)) {
  527. int i, j;
  528. for (i = 0, j = 0; i < 16; i++)
  529. if (mask & (1 << (15 - i)))
  530. cpm1_gc->irq[i] = irq_of_parse_and_map(np, j++);
  531. }
  532. mm_gc = &cpm1_gc->mm_gc;
  533. gc = &mm_gc->gc;
  534. mm_gc->save_regs = cpm1_gpio16_save_regs;
  535. gc->ngpio = 16;
  536. gc->direction_input = cpm1_gpio16_dir_in;
  537. gc->direction_output = cpm1_gpio16_dir_out;
  538. gc->get = cpm1_gpio16_get;
  539. gc->set = cpm1_gpio16_set;
  540. gc->to_irq = cpm1_gpio16_to_irq;
  541. gc->parent = dev;
  542. gc->owner = THIS_MODULE;
  543. return of_mm_gpiochip_add_data(np, mm_gc, cpm1_gc);
  544. }
  545. struct cpm1_gpio32_chip {
  546. struct of_mm_gpio_chip mm_gc;
  547. spinlock_t lock;
  548. /* shadowed data register to clear/set bits safely */
  549. u32 cpdata;
  550. };
  551. static void cpm1_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc)
  552. {
  553. struct cpm1_gpio32_chip *cpm1_gc =
  554. container_of(mm_gc, struct cpm1_gpio32_chip, mm_gc);
  555. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  556. cpm1_gc->cpdata = in_be32(&iop->dat);
  557. }
  558. static int cpm1_gpio32_get(struct gpio_chip *gc, unsigned int gpio)
  559. {
  560. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  561. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  562. u32 pin_mask;
  563. pin_mask = 1 << (31 - gpio);
  564. return !!(in_be32(&iop->dat) & pin_mask);
  565. }
  566. static void __cpm1_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,
  567. int value)
  568. {
  569. struct cpm1_gpio32_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  570. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  571. if (value)
  572. cpm1_gc->cpdata |= pin_mask;
  573. else
  574. cpm1_gc->cpdata &= ~pin_mask;
  575. out_be32(&iop->dat, cpm1_gc->cpdata);
  576. }
  577. static void cpm1_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
  578. {
  579. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  580. struct cpm1_gpio32_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  581. unsigned long flags;
  582. u32 pin_mask = 1 << (31 - gpio);
  583. spin_lock_irqsave(&cpm1_gc->lock, flags);
  584. __cpm1_gpio32_set(mm_gc, pin_mask, value);
  585. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  586. }
  587. static int cpm1_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  588. {
  589. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  590. struct cpm1_gpio32_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  591. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  592. unsigned long flags;
  593. u32 pin_mask = 1 << (31 - gpio);
  594. spin_lock_irqsave(&cpm1_gc->lock, flags);
  595. setbits32(&iop->dir, pin_mask);
  596. __cpm1_gpio32_set(mm_gc, pin_mask, val);
  597. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  598. return 0;
  599. }
  600. static int cpm1_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)
  601. {
  602. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  603. struct cpm1_gpio32_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  604. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  605. unsigned long flags;
  606. u32 pin_mask = 1 << (31 - gpio);
  607. spin_lock_irqsave(&cpm1_gc->lock, flags);
  608. clrbits32(&iop->dir, pin_mask);
  609. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  610. return 0;
  611. }
  612. int cpm1_gpiochip_add32(struct device *dev)
  613. {
  614. struct device_node *np = dev->of_node;
  615. struct cpm1_gpio32_chip *cpm1_gc;
  616. struct of_mm_gpio_chip *mm_gc;
  617. struct gpio_chip *gc;
  618. cpm1_gc = kzalloc(sizeof(*cpm1_gc), GFP_KERNEL);
  619. if (!cpm1_gc)
  620. return -ENOMEM;
  621. spin_lock_init(&cpm1_gc->lock);
  622. mm_gc = &cpm1_gc->mm_gc;
  623. gc = &mm_gc->gc;
  624. mm_gc->save_regs = cpm1_gpio32_save_regs;
  625. gc->ngpio = 32;
  626. gc->direction_input = cpm1_gpio32_dir_in;
  627. gc->direction_output = cpm1_gpio32_dir_out;
  628. gc->get = cpm1_gpio32_get;
  629. gc->set = cpm1_gpio32_set;
  630. gc->parent = dev;
  631. gc->owner = THIS_MODULE;
  632. return of_mm_gpiochip_add_data(np, mm_gc, cpm1_gc);
  633. }
  634. #endif /* CONFIG_8xx_GPIO */