cpm1.c 19 KB

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