cpm_uart_cpm2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * linux/drivers/serial/cpm_uart_cpm2.c
  3. *
  4. * Driver for CPM (SCC/SMC) serial ports; CPM2 definitions
  5. *
  6. * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
  7. * Pantelis Antoniou (panto@intracom.gr) (CPM1)
  8. *
  9. * Copyright (C) 2004 Freescale Semiconductor, Inc.
  10. * (C) 2004 Intracom, S.A.
  11. * (C) 2006 MontaVista Software, Inc.
  12. * Vitaly Bordug <vbordug@ru.mvista.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/tty.h>
  31. #include <linux/ioport.h>
  32. #include <linux/init.h>
  33. #include <linux/serial.h>
  34. #include <linux/console.h>
  35. #include <linux/sysrq.h>
  36. #include <linux/device.h>
  37. #include <linux/bootmem.h>
  38. #include <linux/dma-mapping.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. #include <asm/fs_pd.h>
  42. #ifdef CONFIG_PPC_CPM_NEW_BINDING
  43. #include <asm/prom.h>
  44. #endif
  45. #include <linux/serial_core.h>
  46. #include <linux/kernel.h>
  47. #include "cpm_uart.h"
  48. /**************************************************************/
  49. #ifdef CONFIG_PPC_CPM_NEW_BINDING
  50. void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
  51. {
  52. cpm_command(port->command, cmd);
  53. }
  54. void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
  55. struct device_node *np)
  56. {
  57. void __iomem *pram;
  58. unsigned long offset;
  59. struct resource res;
  60. unsigned long len;
  61. /* Don't remap parameter RAM if it has already been initialized
  62. * during console setup.
  63. */
  64. if (IS_SMC(port) && port->smcup)
  65. return port->smcup;
  66. else if (!IS_SMC(port) && port->sccup)
  67. return port->sccup;
  68. if (of_address_to_resource(np, 1, &res))
  69. return NULL;
  70. len = 1 + res.end - res.start;
  71. pram = ioremap(res.start, len);
  72. if (!pram)
  73. return NULL;
  74. if (!IS_SMC(port))
  75. return pram;
  76. if (len != 2) {
  77. printk(KERN_WARNING "cpm_uart[%d]: device tree references "
  78. "SMC pram, using boot loader/wrapper pram mapping. "
  79. "Please fix your device tree to reference the pram "
  80. "base register instead.\n",
  81. port->port.line);
  82. return pram;
  83. }
  84. offset = cpm_dpalloc(PROFF_SMC_SIZE, 64);
  85. out_be16(pram, offset);
  86. iounmap(pram);
  87. return cpm_muram_addr(offset);
  88. }
  89. void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
  90. {
  91. if (!IS_SMC(port))
  92. iounmap(pram);
  93. }
  94. #else
  95. void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
  96. {
  97. ulong val;
  98. int line = port - cpm_uart_ports;
  99. volatile cpm_cpm2_t *cp = cpm2_map(im_cpm);
  100. switch (line) {
  101. case UART_SMC1:
  102. val = mk_cr_cmd(CPM_CR_SMC1_PAGE, CPM_CR_SMC1_SBLOCK, 0,
  103. cmd) | CPM_CR_FLG;
  104. break;
  105. case UART_SMC2:
  106. val = mk_cr_cmd(CPM_CR_SMC2_PAGE, CPM_CR_SMC2_SBLOCK, 0,
  107. cmd) | CPM_CR_FLG;
  108. break;
  109. case UART_SCC1:
  110. val = mk_cr_cmd(CPM_CR_SCC1_PAGE, CPM_CR_SCC1_SBLOCK, 0,
  111. cmd) | CPM_CR_FLG;
  112. break;
  113. case UART_SCC2:
  114. val = mk_cr_cmd(CPM_CR_SCC2_PAGE, CPM_CR_SCC2_SBLOCK, 0,
  115. cmd) | CPM_CR_FLG;
  116. break;
  117. case UART_SCC3:
  118. val = mk_cr_cmd(CPM_CR_SCC3_PAGE, CPM_CR_SCC3_SBLOCK, 0,
  119. cmd) | CPM_CR_FLG;
  120. break;
  121. case UART_SCC4:
  122. val = mk_cr_cmd(CPM_CR_SCC4_PAGE, CPM_CR_SCC4_SBLOCK, 0,
  123. cmd) | CPM_CR_FLG;
  124. break;
  125. default:
  126. return;
  127. }
  128. cp->cp_cpcr = val;
  129. while (cp->cp_cpcr & CPM_CR_FLG) ;
  130. cpm2_unmap(cp);
  131. }
  132. void smc1_lineif(struct uart_cpm_port *pinfo)
  133. {
  134. volatile iop_cpm2_t *io = cpm2_map(im_ioport);
  135. volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
  136. /* SMC1 is only on port D */
  137. io->iop_ppard |= 0x00c00000;
  138. io->iop_pdird |= 0x00400000;
  139. io->iop_pdird &= ~0x00800000;
  140. io->iop_psord &= ~0x00c00000;
  141. /* Wire BRG1 to SMC1 */
  142. cpmux->cmx_smr &= 0x0f;
  143. pinfo->brg = 1;
  144. cpm2_unmap(cpmux);
  145. cpm2_unmap(io);
  146. }
  147. void smc2_lineif(struct uart_cpm_port *pinfo)
  148. {
  149. volatile iop_cpm2_t *io = cpm2_map(im_ioport);
  150. volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
  151. /* SMC2 is only on port A */
  152. io->iop_ppara |= 0x00c00000;
  153. io->iop_pdira |= 0x00400000;
  154. io->iop_pdira &= ~0x00800000;
  155. io->iop_psora &= ~0x00c00000;
  156. /* Wire BRG2 to SMC2 */
  157. cpmux->cmx_smr &= 0xf0;
  158. pinfo->brg = 2;
  159. cpm2_unmap(cpmux);
  160. cpm2_unmap(io);
  161. }
  162. void scc1_lineif(struct uart_cpm_port *pinfo)
  163. {
  164. volatile iop_cpm2_t *io = cpm2_map(im_ioport);
  165. volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
  166. /* Use Port D for SCC1 instead of other functions. */
  167. io->iop_ppard |= 0x00000003;
  168. io->iop_psord &= ~0x00000001; /* Rx */
  169. io->iop_psord |= 0x00000002; /* Tx */
  170. io->iop_pdird &= ~0x00000001; /* Rx */
  171. io->iop_pdird |= 0x00000002; /* Tx */
  172. /* Wire BRG1 to SCC1 */
  173. cpmux->cmx_scr &= 0x00ffffff;
  174. cpmux->cmx_scr |= 0x00000000;
  175. pinfo->brg = 1;
  176. cpm2_unmap(cpmux);
  177. cpm2_unmap(io);
  178. }
  179. void scc2_lineif(struct uart_cpm_port *pinfo)
  180. {
  181. /*
  182. * STx GP3 uses the SCC2 secondary option pin assignment
  183. * which this driver doesn't account for in the static
  184. * pin assignments. This kind of board specific info
  185. * really has to get out of the driver so boards can
  186. * be supported in a sane fashion.
  187. */
  188. volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
  189. #ifndef CONFIG_STX_GP3
  190. volatile iop_cpm2_t *io = cpm2_map(im_ioport);
  191. io->iop_pparb |= 0x008b0000;
  192. io->iop_pdirb |= 0x00880000;
  193. io->iop_psorb |= 0x00880000;
  194. io->iop_pdirb &= ~0x00030000;
  195. io->iop_psorb &= ~0x00030000;
  196. #endif
  197. cpmux->cmx_scr &= 0xff00ffff;
  198. cpmux->cmx_scr |= 0x00090000;
  199. pinfo->brg = 2;
  200. cpm2_unmap(cpmux);
  201. cpm2_unmap(io);
  202. }
  203. void scc3_lineif(struct uart_cpm_port *pinfo)
  204. {
  205. volatile iop_cpm2_t *io = cpm2_map(im_ioport);
  206. volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
  207. io->iop_pparb |= 0x008b0000;
  208. io->iop_pdirb |= 0x00880000;
  209. io->iop_psorb |= 0x00880000;
  210. io->iop_pdirb &= ~0x00030000;
  211. io->iop_psorb &= ~0x00030000;
  212. cpmux->cmx_scr &= 0xffff00ff;
  213. cpmux->cmx_scr |= 0x00001200;
  214. pinfo->brg = 3;
  215. cpm2_unmap(cpmux);
  216. cpm2_unmap(io);
  217. }
  218. void scc4_lineif(struct uart_cpm_port *pinfo)
  219. {
  220. volatile iop_cpm2_t *io = cpm2_map(im_ioport);
  221. volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
  222. io->iop_ppard |= 0x00000600;
  223. io->iop_psord &= ~0x00000600; /* Tx/Rx */
  224. io->iop_pdird &= ~0x00000200; /* Rx */
  225. io->iop_pdird |= 0x00000400; /* Tx */
  226. cpmux->cmx_scr &= 0xffffff00;
  227. cpmux->cmx_scr |= 0x0000001b;
  228. pinfo->brg = 4;
  229. cpm2_unmap(cpmux);
  230. cpm2_unmap(io);
  231. }
  232. #endif
  233. /*
  234. * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
  235. * receive buffer descriptors from dual port ram, and a character
  236. * buffer area from host mem. If we are allocating for the console we need
  237. * to do it from bootmem
  238. */
  239. int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
  240. {
  241. int dpmemsz, memsz;
  242. u8 __iomem *dp_mem;
  243. unsigned long dp_offset;
  244. u8 *mem_addr;
  245. dma_addr_t dma_addr = 0;
  246. pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
  247. dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
  248. dp_offset = cpm_dpalloc(dpmemsz, 8);
  249. if (IS_ERR_VALUE(dp_offset)) {
  250. printk(KERN_ERR
  251. "cpm_uart_cpm.c: could not allocate buffer descriptors\n");
  252. return -ENOMEM;
  253. }
  254. dp_mem = cpm_dpram_addr(dp_offset);
  255. memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
  256. L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
  257. if (is_con) {
  258. mem_addr = alloc_bootmem(memsz);
  259. dma_addr = virt_to_bus(mem_addr);
  260. }
  261. else
  262. mem_addr = dma_alloc_coherent(NULL, memsz, &dma_addr,
  263. GFP_KERNEL);
  264. if (mem_addr == NULL) {
  265. cpm_dpfree(dp_offset);
  266. printk(KERN_ERR
  267. "cpm_uart_cpm.c: could not allocate coherent memory\n");
  268. return -ENOMEM;
  269. }
  270. pinfo->dp_addr = dp_offset;
  271. pinfo->mem_addr = mem_addr;
  272. pinfo->dma_addr = dma_addr;
  273. pinfo->mem_size = memsz;
  274. pinfo->rx_buf = mem_addr;
  275. pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
  276. * pinfo->rx_fifosize);
  277. pinfo->rx_bd_base = (cbd_t __iomem *)dp_mem;
  278. pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
  279. return 0;
  280. }
  281. void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
  282. {
  283. dma_free_coherent(NULL, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
  284. pinfo->rx_fifosize) +
  285. L1_CACHE_ALIGN(pinfo->tx_nrfifos *
  286. pinfo->tx_fifosize), (void __force *)pinfo->mem_addr,
  287. pinfo->dma_addr);
  288. cpm_dpfree(pinfo->dp_addr);
  289. }
  290. #ifndef CONFIG_PPC_CPM_NEW_BINDING
  291. /* Setup any dynamic params in the uart desc */
  292. int cpm_uart_init_portdesc(void)
  293. {
  294. #if defined(CONFIG_SERIAL_CPM_SMC1) || defined(CONFIG_SERIAL_CPM_SMC2)
  295. u16 *addr;
  296. #endif
  297. pr_debug("CPM uart[-]:init portdesc\n");
  298. cpm_uart_nr = 0;
  299. #ifdef CONFIG_SERIAL_CPM_SMC1
  300. cpm_uart_ports[UART_SMC1].smcp = (smc_t *) cpm2_map(im_smc[0]);
  301. cpm_uart_ports[UART_SMC1].port.mapbase =
  302. (unsigned long)cpm_uart_ports[UART_SMC1].smcp;
  303. cpm_uart_ports[UART_SMC1].smcup =
  304. (smc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SMC1], PROFF_SMC_SIZE);
  305. addr = (u16 *)cpm2_map_size(im_dprambase[PROFF_SMC1_BASE], 2);
  306. *addr = PROFF_SMC1;
  307. cpm2_unmap(addr);
  308. cpm_uart_ports[UART_SMC1].smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
  309. cpm_uart_ports[UART_SMC1].smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
  310. cpm_uart_ports[UART_SMC1].port.uartclk = uart_clock();
  311. cpm_uart_port_map[cpm_uart_nr++] = UART_SMC1;
  312. #endif
  313. #ifdef CONFIG_SERIAL_CPM_SMC2
  314. cpm_uart_ports[UART_SMC2].smcp = (smc_t *) cpm2_map(im_smc[1]);
  315. cpm_uart_ports[UART_SMC2].port.mapbase =
  316. (unsigned long)cpm_uart_ports[UART_SMC2].smcp;
  317. cpm_uart_ports[UART_SMC2].smcup =
  318. (smc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SMC2], PROFF_SMC_SIZE);
  319. addr = (u16 *)cpm2_map_size(im_dprambase[PROFF_SMC2_BASE], 2);
  320. *addr = PROFF_SMC2;
  321. cpm2_unmap(addr);
  322. cpm_uart_ports[UART_SMC2].smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
  323. cpm_uart_ports[UART_SMC2].smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
  324. cpm_uart_ports[UART_SMC2].port.uartclk = uart_clock();
  325. cpm_uart_port_map[cpm_uart_nr++] = UART_SMC2;
  326. #endif
  327. #ifdef CONFIG_SERIAL_CPM_SCC1
  328. cpm_uart_ports[UART_SCC1].sccp = (scc_t *) cpm2_map(im_scc[0]);
  329. cpm_uart_ports[UART_SCC1].port.mapbase =
  330. (unsigned long)cpm_uart_ports[UART_SCC1].sccp;
  331. cpm_uart_ports[UART_SCC1].sccup =
  332. (scc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SCC1], PROFF_SCC_SIZE);
  333. cpm_uart_ports[UART_SCC1].sccp->scc_sccm &=
  334. ~(UART_SCCM_TX | UART_SCCM_RX);
  335. cpm_uart_ports[UART_SCC1].sccp->scc_gsmrl &=
  336. ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  337. cpm_uart_ports[UART_SCC1].port.uartclk = uart_clock();
  338. cpm_uart_port_map[cpm_uart_nr++] = UART_SCC1;
  339. #endif
  340. #ifdef CONFIG_SERIAL_CPM_SCC2
  341. cpm_uart_ports[UART_SCC2].sccp = (scc_t *) cpm2_map(im_scc[1]);
  342. cpm_uart_ports[UART_SCC2].port.mapbase =
  343. (unsigned long)cpm_uart_ports[UART_SCC2].sccp;
  344. cpm_uart_ports[UART_SCC2].sccup =
  345. (scc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SCC2], PROFF_SCC_SIZE);
  346. cpm_uart_ports[UART_SCC2].sccp->scc_sccm &=
  347. ~(UART_SCCM_TX | UART_SCCM_RX);
  348. cpm_uart_ports[UART_SCC2].sccp->scc_gsmrl &=
  349. ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  350. cpm_uart_ports[UART_SCC2].port.uartclk = uart_clock();
  351. cpm_uart_port_map[cpm_uart_nr++] = UART_SCC2;
  352. #endif
  353. #ifdef CONFIG_SERIAL_CPM_SCC3
  354. cpm_uart_ports[UART_SCC3].sccp = (scc_t *) cpm2_map(im_scc[2]);
  355. cpm_uart_ports[UART_SCC3].port.mapbase =
  356. (unsigned long)cpm_uart_ports[UART_SCC3].sccp;
  357. cpm_uart_ports[UART_SCC3].sccup =
  358. (scc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SCC3], PROFF_SCC_SIZE);
  359. cpm_uart_ports[UART_SCC3].sccp->scc_sccm &=
  360. ~(UART_SCCM_TX | UART_SCCM_RX);
  361. cpm_uart_ports[UART_SCC3].sccp->scc_gsmrl &=
  362. ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  363. cpm_uart_ports[UART_SCC3].port.uartclk = uart_clock();
  364. cpm_uart_port_map[cpm_uart_nr++] = UART_SCC3;
  365. #endif
  366. #ifdef CONFIG_SERIAL_CPM_SCC4
  367. cpm_uart_ports[UART_SCC4].sccp = (scc_t *) cpm2_map(im_scc[3]);
  368. cpm_uart_ports[UART_SCC4].port.mapbase =
  369. (unsigned long)cpm_uart_ports[UART_SCC4].sccp;
  370. cpm_uart_ports[UART_SCC4].sccup =
  371. (scc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SCC4], PROFF_SCC_SIZE);
  372. cpm_uart_ports[UART_SCC4].sccp->scc_sccm &=
  373. ~(UART_SCCM_TX | UART_SCCM_RX);
  374. cpm_uart_ports[UART_SCC4].sccp->scc_gsmrl &=
  375. ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  376. cpm_uart_ports[UART_SCC4].port.uartclk = uart_clock();
  377. cpm_uart_port_map[cpm_uart_nr++] = UART_SCC4;
  378. #endif
  379. return 0;
  380. }
  381. #endif