cpm1.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 ([email protected])
  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 <linux/of_irq.h>
  37. #include <asm/page.h>
  38. #include <asm/8xx_immap.h>
  39. #include <asm/cpm1.h>
  40. #include <asm/io.h>
  41. #include <asm/rheap.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 = (void __iomem *)VIRT_IMMR_BASE;
  50. void __init cpm_reset(void)
  51. {
  52. sysconf8xx_t __iomem *siu_conf;
  53. cpmp = &mpc8xx_immr->im_cpm;
  54. #ifndef CONFIG_PPC_EARLY_DEBUG_CPM
  55. /* Perform a reset. */
  56. out_be16(&cpmp->cp_cpcr, CPM_CR_RST | CPM_CR_FLG);
  57. /* Wait for it. */
  58. while (in_be16(&cpmp->cp_cpcr) & CPM_CR_FLG);
  59. #endif
  60. #ifdef CONFIG_UCODE_PATCH
  61. cpm_load_patch(cpmp);
  62. #endif
  63. /*
  64. * Set SDMA Bus Request priority 5.
  65. * On 860T, this also enables FEC priority 6. I am not sure
  66. * this is what we really want for some applications, but the
  67. * manual recommends it.
  68. * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
  69. */
  70. siu_conf = immr_map(im_siu_conf);
  71. if ((mfspr(SPRN_IMMR) & 0xffff) == 0x0900) /* MPC885 */
  72. out_be32(&siu_conf->sc_sdcr, 0x40);
  73. else
  74. out_be32(&siu_conf->sc_sdcr, 1);
  75. immr_unmap(siu_conf);
  76. }
  77. static DEFINE_SPINLOCK(cmd_lock);
  78. #define MAX_CR_CMD_LOOPS 10000
  79. int cpm_command(u32 command, u8 opcode)
  80. {
  81. int i, ret;
  82. unsigned long flags;
  83. if (command & 0xffffff0f)
  84. return -EINVAL;
  85. spin_lock_irqsave(&cmd_lock, flags);
  86. ret = 0;
  87. out_be16(&cpmp->cp_cpcr, command | CPM_CR_FLG | (opcode << 8));
  88. for (i = 0; i < MAX_CR_CMD_LOOPS; i++)
  89. if ((in_be16(&cpmp->cp_cpcr) & CPM_CR_FLG) == 0)
  90. goto out;
  91. printk(KERN_ERR "%s(): Not able to issue CPM command\n", __func__);
  92. ret = -EIO;
  93. out:
  94. spin_unlock_irqrestore(&cmd_lock, flags);
  95. return ret;
  96. }
  97. EXPORT_SYMBOL(cpm_command);
  98. /*
  99. * Set a baud rate generator. This needs lots of work. There are
  100. * four BRGs, any of which can be wired to any channel.
  101. * The internal baud rate clock is the system clock divided by 16.
  102. * This assumes the baudrate is 16x oversampled by the uart.
  103. */
  104. #define BRG_INT_CLK (get_brgfreq())
  105. #define BRG_UART_CLK (BRG_INT_CLK/16)
  106. #define BRG_UART_CLK_DIV16 (BRG_UART_CLK/16)
  107. void
  108. cpm_setbrg(uint brg, uint rate)
  109. {
  110. u32 __iomem *bp;
  111. /* This is good enough to get SMCs running..... */
  112. bp = &cpmp->cp_brgc1;
  113. bp += brg;
  114. /*
  115. * The BRG has a 12-bit counter. For really slow baud rates (or
  116. * really fast processors), we may have to further divide by 16.
  117. */
  118. if (((BRG_UART_CLK / rate) - 1) < 4096)
  119. out_be32(bp, (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN);
  120. else
  121. out_be32(bp, (((BRG_UART_CLK_DIV16 / rate) - 1) << 1) |
  122. CPM_BRG_EN | CPM_BRG_DIV16);
  123. }
  124. EXPORT_SYMBOL(cpm_setbrg);
  125. struct cpm_ioport16 {
  126. __be16 dir, par, odr_sor, dat, intr;
  127. __be16 res[3];
  128. };
  129. struct cpm_ioport32b {
  130. __be32 dir, par, odr, dat;
  131. };
  132. struct cpm_ioport32e {
  133. __be32 dir, par, sor, odr, dat;
  134. };
  135. static void __init cpm1_set_pin32(int port, int pin, int flags)
  136. {
  137. struct cpm_ioport32e __iomem *iop;
  138. pin = 1 << (31 - pin);
  139. if (port == CPM_PORTB)
  140. iop = (struct cpm_ioport32e __iomem *)
  141. &mpc8xx_immr->im_cpm.cp_pbdir;
  142. else
  143. iop = (struct cpm_ioport32e __iomem *)
  144. &mpc8xx_immr->im_cpm.cp_pedir;
  145. if (flags & CPM_PIN_OUTPUT)
  146. setbits32(&iop->dir, pin);
  147. else
  148. clrbits32(&iop->dir, pin);
  149. if (!(flags & CPM_PIN_GPIO))
  150. setbits32(&iop->par, pin);
  151. else
  152. clrbits32(&iop->par, pin);
  153. if (port == CPM_PORTB) {
  154. if (flags & CPM_PIN_OPENDRAIN)
  155. setbits16(&mpc8xx_immr->im_cpm.cp_pbodr, pin);
  156. else
  157. clrbits16(&mpc8xx_immr->im_cpm.cp_pbodr, pin);
  158. }
  159. if (port == CPM_PORTE) {
  160. if (flags & CPM_PIN_SECONDARY)
  161. setbits32(&iop->sor, pin);
  162. else
  163. clrbits32(&iop->sor, pin);
  164. if (flags & CPM_PIN_OPENDRAIN)
  165. setbits32(&mpc8xx_immr->im_cpm.cp_peodr, pin);
  166. else
  167. clrbits32(&mpc8xx_immr->im_cpm.cp_peodr, pin);
  168. }
  169. }
  170. static void __init cpm1_set_pin16(int port, int pin, int flags)
  171. {
  172. struct cpm_ioport16 __iomem *iop =
  173. (struct cpm_ioport16 __iomem *)&mpc8xx_immr->im_ioport;
  174. pin = 1 << (15 - pin);
  175. if (port != 0)
  176. iop += port - 1;
  177. if (flags & CPM_PIN_OUTPUT)
  178. setbits16(&iop->dir, pin);
  179. else
  180. clrbits16(&iop->dir, pin);
  181. if (!(flags & CPM_PIN_GPIO))
  182. setbits16(&iop->par, pin);
  183. else
  184. clrbits16(&iop->par, pin);
  185. if (port == CPM_PORTA) {
  186. if (flags & CPM_PIN_OPENDRAIN)
  187. setbits16(&iop->odr_sor, pin);
  188. else
  189. clrbits16(&iop->odr_sor, pin);
  190. }
  191. if (port == CPM_PORTC) {
  192. if (flags & CPM_PIN_SECONDARY)
  193. setbits16(&iop->odr_sor, pin);
  194. else
  195. clrbits16(&iop->odr_sor, pin);
  196. if (flags & CPM_PIN_FALLEDGE)
  197. setbits16(&iop->intr, pin);
  198. else
  199. clrbits16(&iop->intr, pin);
  200. }
  201. }
  202. void __init cpm1_set_pin(enum cpm_port port, int pin, int flags)
  203. {
  204. if (port == CPM_PORTB || port == CPM_PORTE)
  205. cpm1_set_pin32(port, pin, flags);
  206. else
  207. cpm1_set_pin16(port, pin, flags);
  208. }
  209. int __init cpm1_clk_setup(enum cpm_clk_target target, int clock, int mode)
  210. {
  211. int shift;
  212. int i, bits = 0;
  213. u32 __iomem *reg;
  214. u32 mask = 7;
  215. u8 clk_map[][3] = {
  216. {CPM_CLK_SCC1, CPM_BRG1, 0},
  217. {CPM_CLK_SCC1, CPM_BRG2, 1},
  218. {CPM_CLK_SCC1, CPM_BRG3, 2},
  219. {CPM_CLK_SCC1, CPM_BRG4, 3},
  220. {CPM_CLK_SCC1, CPM_CLK1, 4},
  221. {CPM_CLK_SCC1, CPM_CLK2, 5},
  222. {CPM_CLK_SCC1, CPM_CLK3, 6},
  223. {CPM_CLK_SCC1, CPM_CLK4, 7},
  224. {CPM_CLK_SCC2, CPM_BRG1, 0},
  225. {CPM_CLK_SCC2, CPM_BRG2, 1},
  226. {CPM_CLK_SCC2, CPM_BRG3, 2},
  227. {CPM_CLK_SCC2, CPM_BRG4, 3},
  228. {CPM_CLK_SCC2, CPM_CLK1, 4},
  229. {CPM_CLK_SCC2, CPM_CLK2, 5},
  230. {CPM_CLK_SCC2, CPM_CLK3, 6},
  231. {CPM_CLK_SCC2, CPM_CLK4, 7},
  232. {CPM_CLK_SCC3, CPM_BRG1, 0},
  233. {CPM_CLK_SCC3, CPM_BRG2, 1},
  234. {CPM_CLK_SCC3, CPM_BRG3, 2},
  235. {CPM_CLK_SCC3, CPM_BRG4, 3},
  236. {CPM_CLK_SCC3, CPM_CLK5, 4},
  237. {CPM_CLK_SCC3, CPM_CLK6, 5},
  238. {CPM_CLK_SCC3, CPM_CLK7, 6},
  239. {CPM_CLK_SCC3, CPM_CLK8, 7},
  240. {CPM_CLK_SCC4, CPM_BRG1, 0},
  241. {CPM_CLK_SCC4, CPM_BRG2, 1},
  242. {CPM_CLK_SCC4, CPM_BRG3, 2},
  243. {CPM_CLK_SCC4, CPM_BRG4, 3},
  244. {CPM_CLK_SCC4, CPM_CLK5, 4},
  245. {CPM_CLK_SCC4, CPM_CLK6, 5},
  246. {CPM_CLK_SCC4, CPM_CLK7, 6},
  247. {CPM_CLK_SCC4, CPM_CLK8, 7},
  248. {CPM_CLK_SMC1, CPM_BRG1, 0},
  249. {CPM_CLK_SMC1, CPM_BRG2, 1},
  250. {CPM_CLK_SMC1, CPM_BRG3, 2},
  251. {CPM_CLK_SMC1, CPM_BRG4, 3},
  252. {CPM_CLK_SMC1, CPM_CLK1, 4},
  253. {CPM_CLK_SMC1, CPM_CLK2, 5},
  254. {CPM_CLK_SMC1, CPM_CLK3, 6},
  255. {CPM_CLK_SMC1, CPM_CLK4, 7},
  256. {CPM_CLK_SMC2, CPM_BRG1, 0},
  257. {CPM_CLK_SMC2, CPM_BRG2, 1},
  258. {CPM_CLK_SMC2, CPM_BRG3, 2},
  259. {CPM_CLK_SMC2, CPM_BRG4, 3},
  260. {CPM_CLK_SMC2, CPM_CLK5, 4},
  261. {CPM_CLK_SMC2, CPM_CLK6, 5},
  262. {CPM_CLK_SMC2, CPM_CLK7, 6},
  263. {CPM_CLK_SMC2, CPM_CLK8, 7},
  264. };
  265. switch (target) {
  266. case CPM_CLK_SCC1:
  267. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  268. shift = 0;
  269. break;
  270. case CPM_CLK_SCC2:
  271. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  272. shift = 8;
  273. break;
  274. case CPM_CLK_SCC3:
  275. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  276. shift = 16;
  277. break;
  278. case CPM_CLK_SCC4:
  279. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  280. shift = 24;
  281. break;
  282. case CPM_CLK_SMC1:
  283. reg = &mpc8xx_immr->im_cpm.cp_simode;
  284. shift = 12;
  285. break;
  286. case CPM_CLK_SMC2:
  287. reg = &mpc8xx_immr->im_cpm.cp_simode;
  288. shift = 28;
  289. break;
  290. default:
  291. printk(KERN_ERR "cpm1_clock_setup: invalid clock target\n");
  292. return -EINVAL;
  293. }
  294. for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
  295. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  296. bits = clk_map[i][2];
  297. break;
  298. }
  299. }
  300. if (i == ARRAY_SIZE(clk_map)) {
  301. printk(KERN_ERR "cpm1_clock_setup: invalid clock combination\n");
  302. return -EINVAL;
  303. }
  304. bits <<= shift;
  305. mask <<= shift;
  306. if (reg == &mpc8xx_immr->im_cpm.cp_sicr) {
  307. if (mode == CPM_CLK_RTX) {
  308. bits |= bits << 3;
  309. mask |= mask << 3;
  310. } else if (mode == CPM_CLK_RX) {
  311. bits <<= 3;
  312. mask <<= 3;
  313. }
  314. }
  315. out_be32(reg, (in_be32(reg) & ~mask) | bits);
  316. return 0;
  317. }
  318. /*
  319. * GPIO LIB API implementation
  320. */
  321. #ifdef CONFIG_8xx_GPIO
  322. struct cpm1_gpio16_chip {
  323. struct of_mm_gpio_chip mm_gc;
  324. spinlock_t lock;
  325. /* shadowed data register to clear/set bits safely */
  326. u16 cpdata;
  327. /* IRQ associated with Pins when relevant */
  328. int irq[16];
  329. };
  330. static void cpm1_gpio16_save_regs(struct of_mm_gpio_chip *mm_gc)
  331. {
  332. struct cpm1_gpio16_chip *cpm1_gc =
  333. container_of(mm_gc, struct cpm1_gpio16_chip, mm_gc);
  334. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  335. cpm1_gc->cpdata = in_be16(&iop->dat);
  336. }
  337. static int cpm1_gpio16_get(struct gpio_chip *gc, unsigned int gpio)
  338. {
  339. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  340. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  341. u16 pin_mask;
  342. pin_mask = 1 << (15 - gpio);
  343. return !!(in_be16(&iop->dat) & pin_mask);
  344. }
  345. static void __cpm1_gpio16_set(struct of_mm_gpio_chip *mm_gc, u16 pin_mask,
  346. int value)
  347. {
  348. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  349. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  350. if (value)
  351. cpm1_gc->cpdata |= pin_mask;
  352. else
  353. cpm1_gc->cpdata &= ~pin_mask;
  354. out_be16(&iop->dat, cpm1_gc->cpdata);
  355. }
  356. static void cpm1_gpio16_set(struct gpio_chip *gc, unsigned int gpio, int value)
  357. {
  358. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  359. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  360. unsigned long flags;
  361. u16 pin_mask = 1 << (15 - gpio);
  362. spin_lock_irqsave(&cpm1_gc->lock, flags);
  363. __cpm1_gpio16_set(mm_gc, pin_mask, value);
  364. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  365. }
  366. static int cpm1_gpio16_to_irq(struct gpio_chip *gc, unsigned int gpio)
  367. {
  368. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  369. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  370. return cpm1_gc->irq[gpio] ? : -ENXIO;
  371. }
  372. static int cpm1_gpio16_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  373. {
  374. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  375. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  376. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  377. unsigned long flags;
  378. u16 pin_mask = 1 << (15 - gpio);
  379. spin_lock_irqsave(&cpm1_gc->lock, flags);
  380. setbits16(&iop->dir, pin_mask);
  381. __cpm1_gpio16_set(mm_gc, pin_mask, val);
  382. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  383. return 0;
  384. }
  385. static int cpm1_gpio16_dir_in(struct gpio_chip *gc, unsigned int gpio)
  386. {
  387. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  388. struct cpm1_gpio16_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  389. struct cpm_ioport16 __iomem *iop = mm_gc->regs;
  390. unsigned long flags;
  391. u16 pin_mask = 1 << (15 - gpio);
  392. spin_lock_irqsave(&cpm1_gc->lock, flags);
  393. clrbits16(&iop->dir, pin_mask);
  394. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  395. return 0;
  396. }
  397. int cpm1_gpiochip_add16(struct device *dev)
  398. {
  399. struct device_node *np = dev->of_node;
  400. struct cpm1_gpio16_chip *cpm1_gc;
  401. struct of_mm_gpio_chip *mm_gc;
  402. struct gpio_chip *gc;
  403. u16 mask;
  404. cpm1_gc = kzalloc(sizeof(*cpm1_gc), GFP_KERNEL);
  405. if (!cpm1_gc)
  406. return -ENOMEM;
  407. spin_lock_init(&cpm1_gc->lock);
  408. if (!of_property_read_u16(np, "fsl,cpm1-gpio-irq-mask", &mask)) {
  409. int i, j;
  410. for (i = 0, j = 0; i < 16; i++)
  411. if (mask & (1 << (15 - i)))
  412. cpm1_gc->irq[i] = irq_of_parse_and_map(np, j++);
  413. }
  414. mm_gc = &cpm1_gc->mm_gc;
  415. gc = &mm_gc->gc;
  416. mm_gc->save_regs = cpm1_gpio16_save_regs;
  417. gc->ngpio = 16;
  418. gc->direction_input = cpm1_gpio16_dir_in;
  419. gc->direction_output = cpm1_gpio16_dir_out;
  420. gc->get = cpm1_gpio16_get;
  421. gc->set = cpm1_gpio16_set;
  422. gc->to_irq = cpm1_gpio16_to_irq;
  423. gc->parent = dev;
  424. gc->owner = THIS_MODULE;
  425. return of_mm_gpiochip_add_data(np, mm_gc, cpm1_gc);
  426. }
  427. struct cpm1_gpio32_chip {
  428. struct of_mm_gpio_chip mm_gc;
  429. spinlock_t lock;
  430. /* shadowed data register to clear/set bits safely */
  431. u32 cpdata;
  432. };
  433. static void cpm1_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc)
  434. {
  435. struct cpm1_gpio32_chip *cpm1_gc =
  436. container_of(mm_gc, struct cpm1_gpio32_chip, mm_gc);
  437. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  438. cpm1_gc->cpdata = in_be32(&iop->dat);
  439. }
  440. static int cpm1_gpio32_get(struct gpio_chip *gc, unsigned int gpio)
  441. {
  442. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  443. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  444. u32 pin_mask;
  445. pin_mask = 1 << (31 - gpio);
  446. return !!(in_be32(&iop->dat) & pin_mask);
  447. }
  448. static void __cpm1_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,
  449. int value)
  450. {
  451. struct cpm1_gpio32_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  452. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  453. if (value)
  454. cpm1_gc->cpdata |= pin_mask;
  455. else
  456. cpm1_gc->cpdata &= ~pin_mask;
  457. out_be32(&iop->dat, cpm1_gc->cpdata);
  458. }
  459. static void cpm1_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
  460. {
  461. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  462. struct cpm1_gpio32_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  463. unsigned long flags;
  464. u32 pin_mask = 1 << (31 - gpio);
  465. spin_lock_irqsave(&cpm1_gc->lock, flags);
  466. __cpm1_gpio32_set(mm_gc, pin_mask, value);
  467. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  468. }
  469. static int cpm1_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  470. {
  471. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  472. struct cpm1_gpio32_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  473. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  474. unsigned long flags;
  475. u32 pin_mask = 1 << (31 - gpio);
  476. spin_lock_irqsave(&cpm1_gc->lock, flags);
  477. setbits32(&iop->dir, pin_mask);
  478. __cpm1_gpio32_set(mm_gc, pin_mask, val);
  479. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  480. return 0;
  481. }
  482. static int cpm1_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)
  483. {
  484. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  485. struct cpm1_gpio32_chip *cpm1_gc = gpiochip_get_data(&mm_gc->gc);
  486. struct cpm_ioport32b __iomem *iop = mm_gc->regs;
  487. unsigned long flags;
  488. u32 pin_mask = 1 << (31 - gpio);
  489. spin_lock_irqsave(&cpm1_gc->lock, flags);
  490. clrbits32(&iop->dir, pin_mask);
  491. spin_unlock_irqrestore(&cpm1_gc->lock, flags);
  492. return 0;
  493. }
  494. int cpm1_gpiochip_add32(struct device *dev)
  495. {
  496. struct device_node *np = dev->of_node;
  497. struct cpm1_gpio32_chip *cpm1_gc;
  498. struct of_mm_gpio_chip *mm_gc;
  499. struct gpio_chip *gc;
  500. cpm1_gc = kzalloc(sizeof(*cpm1_gc), GFP_KERNEL);
  501. if (!cpm1_gc)
  502. return -ENOMEM;
  503. spin_lock_init(&cpm1_gc->lock);
  504. mm_gc = &cpm1_gc->mm_gc;
  505. gc = &mm_gc->gc;
  506. mm_gc->save_regs = cpm1_gpio32_save_regs;
  507. gc->ngpio = 32;
  508. gc->direction_input = cpm1_gpio32_dir_in;
  509. gc->direction_output = cpm1_gpio32_dir_out;
  510. gc->get = cpm1_gpio32_get;
  511. gc->set = cpm1_gpio32_set;
  512. gc->parent = dev;
  513. gc->owner = THIS_MODULE;
  514. return of_mm_gpiochip_add_data(np, mm_gc, cpm1_gc);
  515. }
  516. #endif /* CONFIG_8xx_GPIO */