cpm1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 <asm/page.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/8xx_immap.h>
  36. #include <asm/cpm1.h>
  37. #include <asm/io.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/rheap.h>
  40. #include <asm/prom.h>
  41. #include <asm/cpm.h>
  42. #include <asm/fs_pd.h>
  43. #define CPM_MAP_SIZE (0x4000)
  44. cpm8xx_t __iomem *cpmp; /* Pointer to comm processor space */
  45. immap_t __iomem *mpc8xx_immr;
  46. static cpic8xx_t __iomem *cpic_reg;
  47. static struct irq_host *cpm_pic_host;
  48. static void cpm_mask_irq(unsigned int irq)
  49. {
  50. unsigned int cpm_vec = (unsigned int)irq_map[irq].hwirq;
  51. clrbits32(&cpic_reg->cpic_cimr, (1 << cpm_vec));
  52. }
  53. static void cpm_unmask_irq(unsigned int irq)
  54. {
  55. unsigned int cpm_vec = (unsigned int)irq_map[irq].hwirq;
  56. setbits32(&cpic_reg->cpic_cimr, (1 << cpm_vec));
  57. }
  58. static void cpm_end_irq(unsigned int irq)
  59. {
  60. unsigned int cpm_vec = (unsigned int)irq_map[irq].hwirq;
  61. out_be32(&cpic_reg->cpic_cisr, (1 << cpm_vec));
  62. }
  63. static struct irq_chip cpm_pic = {
  64. .typename = " CPM PIC ",
  65. .mask = cpm_mask_irq,
  66. .unmask = cpm_unmask_irq,
  67. .eoi = cpm_end_irq,
  68. };
  69. int cpm_get_irq(void)
  70. {
  71. int cpm_vec;
  72. /* Get the vector by setting the ACK bit and then reading
  73. * the register.
  74. */
  75. out_be16(&cpic_reg->cpic_civr, 1);
  76. cpm_vec = in_be16(&cpic_reg->cpic_civr);
  77. cpm_vec >>= 11;
  78. return irq_linear_revmap(cpm_pic_host, cpm_vec);
  79. }
  80. static int cpm_pic_host_map(struct irq_host *h, unsigned int virq,
  81. irq_hw_number_t hw)
  82. {
  83. pr_debug("cpm_pic_host_map(%d, 0x%lx)\n", virq, hw);
  84. get_irq_desc(virq)->status |= IRQ_LEVEL;
  85. set_irq_chip_and_handler(virq, &cpm_pic, handle_fasteoi_irq);
  86. return 0;
  87. }
  88. /* The CPM can generate the error interrupt when there is a race condition
  89. * between generating and masking interrupts. All we have to do is ACK it
  90. * and return. This is a no-op function so we don't need any special
  91. * tests in the interrupt handler.
  92. */
  93. static irqreturn_t cpm_error_interrupt(int irq, void *dev)
  94. {
  95. return IRQ_HANDLED;
  96. }
  97. static struct irqaction cpm_error_irqaction = {
  98. .handler = cpm_error_interrupt,
  99. .mask = CPU_MASK_NONE,
  100. .name = "error",
  101. };
  102. static struct irq_host_ops cpm_pic_host_ops = {
  103. .map = cpm_pic_host_map,
  104. };
  105. unsigned int cpm_pic_init(void)
  106. {
  107. struct device_node *np = NULL;
  108. struct resource res;
  109. unsigned int sirq = NO_IRQ, hwirq, eirq;
  110. int ret;
  111. pr_debug("cpm_pic_init\n");
  112. np = of_find_compatible_node(NULL, NULL, "fsl,cpm1-pic");
  113. if (np == NULL)
  114. np = of_find_compatible_node(NULL, "cpm-pic", "CPM");
  115. if (np == NULL) {
  116. printk(KERN_ERR "CPM PIC init: can not find cpm-pic node\n");
  117. return sirq;
  118. }
  119. ret = of_address_to_resource(np, 0, &res);
  120. if (ret)
  121. goto end;
  122. cpic_reg = ioremap(res.start, res.end - res.start + 1);
  123. if (cpic_reg == NULL)
  124. goto end;
  125. sirq = irq_of_parse_and_map(np, 0);
  126. if (sirq == NO_IRQ)
  127. goto end;
  128. /* Initialize the CPM interrupt controller. */
  129. hwirq = (unsigned int)irq_map[sirq].hwirq;
  130. out_be32(&cpic_reg->cpic_cicr,
  131. (CICR_SCD_SCC4 | CICR_SCC_SCC3 | CICR_SCB_SCC2 | CICR_SCA_SCC1) |
  132. ((hwirq/2) << 13) | CICR_HP_MASK);
  133. out_be32(&cpic_reg->cpic_cimr, 0);
  134. cpm_pic_host = irq_alloc_host(of_node_get(np), IRQ_HOST_MAP_LINEAR,
  135. 64, &cpm_pic_host_ops, 64);
  136. if (cpm_pic_host == NULL) {
  137. printk(KERN_ERR "CPM2 PIC: failed to allocate irq host!\n");
  138. sirq = NO_IRQ;
  139. goto end;
  140. }
  141. /* Install our own error handler. */
  142. np = of_find_compatible_node(NULL, NULL, "fsl,cpm1");
  143. if (np == NULL)
  144. np = of_find_node_by_type(NULL, "cpm");
  145. if (np == NULL) {
  146. printk(KERN_ERR "CPM PIC init: can not find cpm node\n");
  147. goto end;
  148. }
  149. eirq = irq_of_parse_and_map(np, 0);
  150. if (eirq == NO_IRQ)
  151. goto end;
  152. if (setup_irq(eirq, &cpm_error_irqaction))
  153. printk(KERN_ERR "Could not allocate CPM error IRQ!");
  154. setbits32(&cpic_reg->cpic_cicr, CICR_IEN);
  155. end:
  156. of_node_put(np);
  157. return sirq;
  158. }
  159. void __init cpm_reset(void)
  160. {
  161. sysconf8xx_t __iomem *siu_conf;
  162. mpc8xx_immr = ioremap(get_immrbase(), 0x4000);
  163. if (!mpc8xx_immr) {
  164. printk(KERN_CRIT "Could not map IMMR\n");
  165. return;
  166. }
  167. cpmp = &mpc8xx_immr->im_cpm;
  168. #ifndef CONFIG_PPC_EARLY_DEBUG_CPM
  169. /* Perform a reset.
  170. */
  171. out_be16(&cpmp->cp_cpcr, CPM_CR_RST | CPM_CR_FLG);
  172. /* Wait for it.
  173. */
  174. while (in_be16(&cpmp->cp_cpcr) & CPM_CR_FLG);
  175. #endif
  176. #ifdef CONFIG_UCODE_PATCH
  177. cpm_load_patch(cpmp);
  178. #endif
  179. /* Set SDMA Bus Request priority 5.
  180. * On 860T, this also enables FEC priority 6. I am not sure
  181. * this is what we realy want for some applications, but the
  182. * manual recommends it.
  183. * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
  184. */
  185. siu_conf = immr_map(im_siu_conf);
  186. out_be32(&siu_conf->sc_sdcr, 1);
  187. immr_unmap(siu_conf);
  188. cpm_muram_init();
  189. }
  190. static DEFINE_SPINLOCK(cmd_lock);
  191. #define MAX_CR_CMD_LOOPS 10000
  192. int cpm_command(u32 command, u8 opcode)
  193. {
  194. int i, ret;
  195. unsigned long flags;
  196. if (command & 0xffffff0f)
  197. return -EINVAL;
  198. spin_lock_irqsave(&cmd_lock, flags);
  199. ret = 0;
  200. out_be16(&cpmp->cp_cpcr, command | CPM_CR_FLG | (opcode << 8));
  201. for (i = 0; i < MAX_CR_CMD_LOOPS; i++)
  202. if ((in_be16(&cpmp->cp_cpcr) & CPM_CR_FLG) == 0)
  203. goto out;
  204. printk(KERN_ERR "%s(): Not able to issue CPM command\n", __func__);
  205. ret = -EIO;
  206. out:
  207. spin_unlock_irqrestore(&cmd_lock, flags);
  208. return ret;
  209. }
  210. EXPORT_SYMBOL(cpm_command);
  211. /* Set a baud rate generator. This needs lots of work. There are
  212. * four BRGs, any of which can be wired to any channel.
  213. * The internal baud rate clock is the system clock divided by 16.
  214. * This assumes the baudrate is 16x oversampled by the uart.
  215. */
  216. #define BRG_INT_CLK (get_brgfreq())
  217. #define BRG_UART_CLK (BRG_INT_CLK/16)
  218. #define BRG_UART_CLK_DIV16 (BRG_UART_CLK/16)
  219. void
  220. cpm_setbrg(uint brg, uint rate)
  221. {
  222. u32 __iomem *bp;
  223. /* This is good enough to get SMCs running.....
  224. */
  225. bp = &cpmp->cp_brgc1;
  226. bp += brg;
  227. /* The BRG has a 12-bit counter. For really slow baud rates (or
  228. * really fast processors), we may have to further divide by 16.
  229. */
  230. if (((BRG_UART_CLK / rate) - 1) < 4096)
  231. out_be32(bp, (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN);
  232. else
  233. out_be32(bp, (((BRG_UART_CLK_DIV16 / rate) - 1) << 1) |
  234. CPM_BRG_EN | CPM_BRG_DIV16);
  235. }
  236. struct cpm_ioport16 {
  237. __be16 dir, par, odr_sor, dat, intr;
  238. __be16 res[3];
  239. };
  240. struct cpm_ioport32 {
  241. __be32 dir, par, sor;
  242. };
  243. static void cpm1_set_pin32(int port, int pin, int flags)
  244. {
  245. struct cpm_ioport32 __iomem *iop;
  246. pin = 1 << (31 - pin);
  247. if (port == CPM_PORTB)
  248. iop = (struct cpm_ioport32 __iomem *)
  249. &mpc8xx_immr->im_cpm.cp_pbdir;
  250. else
  251. iop = (struct cpm_ioport32 __iomem *)
  252. &mpc8xx_immr->im_cpm.cp_pedir;
  253. if (flags & CPM_PIN_OUTPUT)
  254. setbits32(&iop->dir, pin);
  255. else
  256. clrbits32(&iop->dir, pin);
  257. if (!(flags & CPM_PIN_GPIO))
  258. setbits32(&iop->par, pin);
  259. else
  260. clrbits32(&iop->par, pin);
  261. if (port == CPM_PORTB) {
  262. if (flags & CPM_PIN_OPENDRAIN)
  263. setbits16(&mpc8xx_immr->im_cpm.cp_pbodr, pin);
  264. else
  265. clrbits16(&mpc8xx_immr->im_cpm.cp_pbodr, pin);
  266. }
  267. if (port == CPM_PORTE) {
  268. if (flags & CPM_PIN_SECONDARY)
  269. setbits32(&iop->sor, pin);
  270. else
  271. clrbits32(&iop->sor, pin);
  272. if (flags & CPM_PIN_OPENDRAIN)
  273. setbits32(&mpc8xx_immr->im_cpm.cp_peodr, pin);
  274. else
  275. clrbits32(&mpc8xx_immr->im_cpm.cp_peodr, pin);
  276. }
  277. }
  278. static void cpm1_set_pin16(int port, int pin, int flags)
  279. {
  280. struct cpm_ioport16 __iomem *iop =
  281. (struct cpm_ioport16 __iomem *)&mpc8xx_immr->im_ioport;
  282. pin = 1 << (15 - pin);
  283. if (port != 0)
  284. iop += port - 1;
  285. if (flags & CPM_PIN_OUTPUT)
  286. setbits16(&iop->dir, pin);
  287. else
  288. clrbits16(&iop->dir, pin);
  289. if (!(flags & CPM_PIN_GPIO))
  290. setbits16(&iop->par, pin);
  291. else
  292. clrbits16(&iop->par, pin);
  293. if (port == CPM_PORTA) {
  294. if (flags & CPM_PIN_OPENDRAIN)
  295. setbits16(&iop->odr_sor, pin);
  296. else
  297. clrbits16(&iop->odr_sor, pin);
  298. }
  299. if (port == CPM_PORTC) {
  300. if (flags & CPM_PIN_SECONDARY)
  301. setbits16(&iop->odr_sor, pin);
  302. else
  303. clrbits16(&iop->odr_sor, pin);
  304. }
  305. }
  306. void cpm1_set_pin(enum cpm_port port, int pin, int flags)
  307. {
  308. if (port == CPM_PORTB || port == CPM_PORTE)
  309. cpm1_set_pin32(port, pin, flags);
  310. else
  311. cpm1_set_pin16(port, pin, flags);
  312. }
  313. int cpm1_clk_setup(enum cpm_clk_target target, int clock, int mode)
  314. {
  315. int shift;
  316. int i, bits = 0;
  317. u32 __iomem *reg;
  318. u32 mask = 7;
  319. u8 clk_map[][3] = {
  320. {CPM_CLK_SCC1, CPM_BRG1, 0},
  321. {CPM_CLK_SCC1, CPM_BRG2, 1},
  322. {CPM_CLK_SCC1, CPM_BRG3, 2},
  323. {CPM_CLK_SCC1, CPM_BRG4, 3},
  324. {CPM_CLK_SCC1, CPM_CLK1, 4},
  325. {CPM_CLK_SCC1, CPM_CLK2, 5},
  326. {CPM_CLK_SCC1, CPM_CLK3, 6},
  327. {CPM_CLK_SCC1, CPM_CLK4, 7},
  328. {CPM_CLK_SCC2, CPM_BRG1, 0},
  329. {CPM_CLK_SCC2, CPM_BRG2, 1},
  330. {CPM_CLK_SCC2, CPM_BRG3, 2},
  331. {CPM_CLK_SCC2, CPM_BRG4, 3},
  332. {CPM_CLK_SCC2, CPM_CLK1, 4},
  333. {CPM_CLK_SCC2, CPM_CLK2, 5},
  334. {CPM_CLK_SCC2, CPM_CLK3, 6},
  335. {CPM_CLK_SCC2, CPM_CLK4, 7},
  336. {CPM_CLK_SCC3, CPM_BRG1, 0},
  337. {CPM_CLK_SCC3, CPM_BRG2, 1},
  338. {CPM_CLK_SCC3, CPM_BRG3, 2},
  339. {CPM_CLK_SCC3, CPM_BRG4, 3},
  340. {CPM_CLK_SCC3, CPM_CLK5, 4},
  341. {CPM_CLK_SCC3, CPM_CLK6, 5},
  342. {CPM_CLK_SCC3, CPM_CLK7, 6},
  343. {CPM_CLK_SCC3, CPM_CLK8, 7},
  344. {CPM_CLK_SCC4, CPM_BRG1, 0},
  345. {CPM_CLK_SCC4, CPM_BRG2, 1},
  346. {CPM_CLK_SCC4, CPM_BRG3, 2},
  347. {CPM_CLK_SCC4, CPM_BRG4, 3},
  348. {CPM_CLK_SCC4, CPM_CLK5, 4},
  349. {CPM_CLK_SCC4, CPM_CLK6, 5},
  350. {CPM_CLK_SCC4, CPM_CLK7, 6},
  351. {CPM_CLK_SCC4, CPM_CLK8, 7},
  352. {CPM_CLK_SMC1, CPM_BRG1, 0},
  353. {CPM_CLK_SMC1, CPM_BRG2, 1},
  354. {CPM_CLK_SMC1, CPM_BRG3, 2},
  355. {CPM_CLK_SMC1, CPM_BRG4, 3},
  356. {CPM_CLK_SMC1, CPM_CLK1, 4},
  357. {CPM_CLK_SMC1, CPM_CLK2, 5},
  358. {CPM_CLK_SMC1, CPM_CLK3, 6},
  359. {CPM_CLK_SMC1, CPM_CLK4, 7},
  360. {CPM_CLK_SMC2, CPM_BRG1, 0},
  361. {CPM_CLK_SMC2, CPM_BRG2, 1},
  362. {CPM_CLK_SMC2, CPM_BRG3, 2},
  363. {CPM_CLK_SMC2, CPM_BRG4, 3},
  364. {CPM_CLK_SMC2, CPM_CLK5, 4},
  365. {CPM_CLK_SMC2, CPM_CLK6, 5},
  366. {CPM_CLK_SMC2, CPM_CLK7, 6},
  367. {CPM_CLK_SMC2, CPM_CLK8, 7},
  368. };
  369. switch (target) {
  370. case CPM_CLK_SCC1:
  371. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  372. shift = 0;
  373. break;
  374. case CPM_CLK_SCC2:
  375. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  376. shift = 8;
  377. break;
  378. case CPM_CLK_SCC3:
  379. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  380. shift = 16;
  381. break;
  382. case CPM_CLK_SCC4:
  383. reg = &mpc8xx_immr->im_cpm.cp_sicr;
  384. shift = 24;
  385. break;
  386. case CPM_CLK_SMC1:
  387. reg = &mpc8xx_immr->im_cpm.cp_simode;
  388. shift = 12;
  389. break;
  390. case CPM_CLK_SMC2:
  391. reg = &mpc8xx_immr->im_cpm.cp_simode;
  392. shift = 28;
  393. break;
  394. default:
  395. printk(KERN_ERR "cpm1_clock_setup: invalid clock target\n");
  396. return -EINVAL;
  397. }
  398. if (reg == &mpc8xx_immr->im_cpm.cp_sicr && mode == CPM_CLK_RX)
  399. shift += 3;
  400. for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
  401. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  402. bits = clk_map[i][2];
  403. break;
  404. }
  405. }
  406. if (i == ARRAY_SIZE(clk_map)) {
  407. printk(KERN_ERR "cpm1_clock_setup: invalid clock combination\n");
  408. return -EINVAL;
  409. }
  410. bits <<= shift;
  411. mask <<= shift;
  412. out_be32(reg, (in_be32(reg) & ~mask) | bits);
  413. return 0;
  414. }