cpm1.c 19 KB

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