serial.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * arch/arm/mach-omap2/serial.c
  3. *
  4. * OMAP2 serial support.
  5. *
  6. * Copyright (C) 2005-2008 Nokia Corporation
  7. * Author: Paul Mundt <paul.mundt@nokia.com>
  8. *
  9. * Major rework for PM support by Kevin Hilman
  10. *
  11. * Based off of arch/arm/mach-omap/omap1/serial.c
  12. *
  13. * Copyright (C) 2009 Texas Instruments
  14. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com
  15. *
  16. * This file is subject to the terms and conditions of the GNU General Public
  17. * License. See the file "COPYING" in the main directory of this archive
  18. * for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/serial_8250.h>
  23. #include <linux/serial_reg.h>
  24. #include <linux/clk.h>
  25. #include <linux/io.h>
  26. #include <mach/common.h>
  27. #include <mach/board.h>
  28. #include <mach/clock.h>
  29. #include <mach/control.h>
  30. #include "prm.h"
  31. #include "pm.h"
  32. #include "prm-regbits-34xx.h"
  33. #define UART_OMAP_WER 0x17 /* Wake-up enable register */
  34. #define DEFAULT_TIMEOUT (5 * HZ)
  35. struct omap_uart_state {
  36. int num;
  37. int can_sleep;
  38. struct timer_list timer;
  39. u32 timeout;
  40. void __iomem *wk_st;
  41. void __iomem *wk_en;
  42. u32 wk_mask;
  43. u32 padconf;
  44. struct clk *ick;
  45. struct clk *fck;
  46. int clocked;
  47. struct plat_serial8250_port *p;
  48. struct list_head node;
  49. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
  50. int context_valid;
  51. /* Registers to be saved/restored for OFF-mode */
  52. u16 dll;
  53. u16 dlh;
  54. u16 ier;
  55. u16 sysc;
  56. u16 scr;
  57. u16 wer;
  58. #endif
  59. };
  60. static struct omap_uart_state omap_uart[OMAP_MAX_NR_PORTS];
  61. static LIST_HEAD(uart_list);
  62. static struct plat_serial8250_port serial_platform_data[] = {
  63. {
  64. .membase = IO_ADDRESS(OMAP_UART1_BASE),
  65. .mapbase = OMAP_UART1_BASE,
  66. .irq = 72,
  67. .flags = UPF_BOOT_AUTOCONF,
  68. .iotype = UPIO_MEM,
  69. .regshift = 2,
  70. .uartclk = OMAP24XX_BASE_BAUD * 16,
  71. }, {
  72. .membase = IO_ADDRESS(OMAP_UART2_BASE),
  73. .mapbase = OMAP_UART2_BASE,
  74. .irq = 73,
  75. .flags = UPF_BOOT_AUTOCONF,
  76. .iotype = UPIO_MEM,
  77. .regshift = 2,
  78. .uartclk = OMAP24XX_BASE_BAUD * 16,
  79. }, {
  80. .membase = IO_ADDRESS(OMAP_UART3_BASE),
  81. .mapbase = OMAP_UART3_BASE,
  82. .irq = 74,
  83. .flags = UPF_BOOT_AUTOCONF,
  84. .iotype = UPIO_MEM,
  85. .regshift = 2,
  86. .uartclk = OMAP24XX_BASE_BAUD * 16,
  87. }, {
  88. .flags = 0
  89. }
  90. };
  91. static inline unsigned int serial_read_reg(struct plat_serial8250_port *up,
  92. int offset)
  93. {
  94. offset <<= up->regshift;
  95. return (unsigned int)__raw_readb(up->membase + offset);
  96. }
  97. static inline void serial_write_reg(struct plat_serial8250_port *p, int offset,
  98. int value)
  99. {
  100. offset <<= p->regshift;
  101. __raw_writeb(value, p->membase + offset);
  102. }
  103. /*
  104. * Internal UARTs need to be initialized for the 8250 autoconfig to work
  105. * properly. Note that the TX watermark initialization may not be needed
  106. * once the 8250.c watermark handling code is merged.
  107. */
  108. static inline void __init omap_uart_reset(struct omap_uart_state *uart)
  109. {
  110. struct plat_serial8250_port *p = uart->p;
  111. serial_write_reg(p, UART_OMAP_MDR1, 0x07);
  112. serial_write_reg(p, UART_OMAP_SCR, 0x08);
  113. serial_write_reg(p, UART_OMAP_MDR1, 0x00);
  114. serial_write_reg(p, UART_OMAP_SYSC, (0x02 << 3) | (1 << 2) | (1 << 0));
  115. }
  116. #if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
  117. static int enable_off_mode; /* to be removed by full off-mode patches */
  118. static void omap_uart_save_context(struct omap_uart_state *uart)
  119. {
  120. u16 lcr = 0;
  121. struct plat_serial8250_port *p = uart->p;
  122. if (!enable_off_mode)
  123. return;
  124. lcr = serial_read_reg(p, UART_LCR);
  125. serial_write_reg(p, UART_LCR, 0xBF);
  126. uart->dll = serial_read_reg(p, UART_DLL);
  127. uart->dlh = serial_read_reg(p, UART_DLM);
  128. serial_write_reg(p, UART_LCR, lcr);
  129. uart->ier = serial_read_reg(p, UART_IER);
  130. uart->sysc = serial_read_reg(p, UART_OMAP_SYSC);
  131. uart->scr = serial_read_reg(p, UART_OMAP_SCR);
  132. uart->wer = serial_read_reg(p, UART_OMAP_WER);
  133. uart->context_valid = 1;
  134. }
  135. static void omap_uart_restore_context(struct omap_uart_state *uart)
  136. {
  137. u16 efr = 0;
  138. struct plat_serial8250_port *p = uart->p;
  139. if (!enable_off_mode)
  140. return;
  141. if (!uart->context_valid)
  142. return;
  143. uart->context_valid = 0;
  144. serial_write_reg(p, UART_OMAP_MDR1, 0x7);
  145. serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
  146. efr = serial_read_reg(p, UART_EFR);
  147. serial_write_reg(p, UART_EFR, UART_EFR_ECB);
  148. serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
  149. serial_write_reg(p, UART_IER, 0x0);
  150. serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
  151. serial_write_reg(p, UART_DLL, uart->dll);
  152. serial_write_reg(p, UART_DLM, uart->dlh);
  153. serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
  154. serial_write_reg(p, UART_IER, uart->ier);
  155. serial_write_reg(p, UART_FCR, 0xA1);
  156. serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
  157. serial_write_reg(p, UART_EFR, efr);
  158. serial_write_reg(p, UART_LCR, UART_LCR_WLEN8);
  159. serial_write_reg(p, UART_OMAP_SCR, uart->scr);
  160. serial_write_reg(p, UART_OMAP_WER, uart->wer);
  161. serial_write_reg(p, UART_OMAP_SYSC, uart->sysc);
  162. serial_write_reg(p, UART_OMAP_MDR1, 0x00); /* UART 16x mode */
  163. }
  164. #else
  165. static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
  166. static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
  167. #endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
  168. static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
  169. {
  170. if (uart->clocked)
  171. return;
  172. clk_enable(uart->ick);
  173. clk_enable(uart->fck);
  174. uart->clocked = 1;
  175. omap_uart_restore_context(uart);
  176. }
  177. #ifdef CONFIG_PM
  178. static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
  179. {
  180. if (!uart->clocked)
  181. return;
  182. omap_uart_save_context(uart);
  183. uart->clocked = 0;
  184. clk_disable(uart->ick);
  185. clk_disable(uart->fck);
  186. }
  187. static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
  188. int enable)
  189. {
  190. struct plat_serial8250_port *p = uart->p;
  191. u16 sysc;
  192. sysc = serial_read_reg(p, UART_OMAP_SYSC) & 0x7;
  193. if (enable)
  194. sysc |= 0x2 << 3;
  195. else
  196. sysc |= 0x1 << 3;
  197. serial_write_reg(p, UART_OMAP_SYSC, sysc);
  198. }
  199. static void omap_uart_block_sleep(struct omap_uart_state *uart)
  200. {
  201. omap_uart_enable_clocks(uart);
  202. omap_uart_smart_idle_enable(uart, 0);
  203. uart->can_sleep = 0;
  204. if (uart->timeout)
  205. mod_timer(&uart->timer, jiffies + uart->timeout);
  206. else
  207. del_timer(&uart->timer);
  208. }
  209. static void omap_uart_allow_sleep(struct omap_uart_state *uart)
  210. {
  211. if (!uart->clocked)
  212. return;
  213. omap_uart_smart_idle_enable(uart, 1);
  214. uart->can_sleep = 1;
  215. del_timer(&uart->timer);
  216. }
  217. static void omap_uart_idle_timer(unsigned long data)
  218. {
  219. struct omap_uart_state *uart = (struct omap_uart_state *)data;
  220. omap_uart_allow_sleep(uart);
  221. }
  222. void omap_uart_prepare_idle(int num)
  223. {
  224. struct omap_uart_state *uart;
  225. list_for_each_entry(uart, &uart_list, node) {
  226. if (num == uart->num && uart->can_sleep) {
  227. omap_uart_disable_clocks(uart);
  228. return;
  229. }
  230. }
  231. }
  232. void omap_uart_resume_idle(int num)
  233. {
  234. struct omap_uart_state *uart;
  235. list_for_each_entry(uart, &uart_list, node) {
  236. if (num == uart->num) {
  237. omap_uart_enable_clocks(uart);
  238. /* Check for IO pad wakeup */
  239. if (cpu_is_omap34xx() && uart->padconf) {
  240. u16 p = omap_ctrl_readw(uart->padconf);
  241. if (p & OMAP3_PADCONF_WAKEUPEVENT0)
  242. omap_uart_block_sleep(uart);
  243. }
  244. /* Check for normal UART wakeup */
  245. if (__raw_readl(uart->wk_st) & uart->wk_mask)
  246. omap_uart_block_sleep(uart);
  247. return;
  248. }
  249. }
  250. }
  251. void omap_uart_prepare_suspend(void)
  252. {
  253. struct omap_uart_state *uart;
  254. list_for_each_entry(uart, &uart_list, node) {
  255. omap_uart_allow_sleep(uart);
  256. }
  257. }
  258. int omap_uart_can_sleep(void)
  259. {
  260. struct omap_uart_state *uart;
  261. int can_sleep = 1;
  262. list_for_each_entry(uart, &uart_list, node) {
  263. if (!uart->clocked)
  264. continue;
  265. if (!uart->can_sleep) {
  266. can_sleep = 0;
  267. continue;
  268. }
  269. /* This UART can now safely sleep. */
  270. omap_uart_allow_sleep(uart);
  271. }
  272. return can_sleep;
  273. }
  274. /**
  275. * omap_uart_interrupt()
  276. *
  277. * This handler is used only to detect that *any* UART interrupt has
  278. * occurred. It does _nothing_ to handle the interrupt. Rather,
  279. * any UART interrupt will trigger the inactivity timer so the
  280. * UART will not idle or sleep for its timeout period.
  281. *
  282. **/
  283. static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
  284. {
  285. struct omap_uart_state *uart = dev_id;
  286. omap_uart_block_sleep(uart);
  287. return IRQ_NONE;
  288. }
  289. static u32 sleep_timeout = DEFAULT_TIMEOUT;
  290. static void omap_uart_idle_init(struct omap_uart_state *uart)
  291. {
  292. u32 v;
  293. struct plat_serial8250_port *p = uart->p;
  294. int ret;
  295. uart->can_sleep = 0;
  296. uart->timeout = sleep_timeout;
  297. setup_timer(&uart->timer, omap_uart_idle_timer,
  298. (unsigned long) uart);
  299. mod_timer(&uart->timer, jiffies + uart->timeout);
  300. omap_uart_smart_idle_enable(uart, 0);
  301. if (cpu_is_omap34xx()) {
  302. u32 mod = (uart->num == 2) ? OMAP3430_PER_MOD : CORE_MOD;
  303. u32 wk_mask = 0;
  304. u32 padconf = 0;
  305. uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
  306. uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
  307. switch (uart->num) {
  308. case 0:
  309. wk_mask = OMAP3430_ST_UART1_MASK;
  310. padconf = 0x182;
  311. break;
  312. case 1:
  313. wk_mask = OMAP3430_ST_UART2_MASK;
  314. padconf = 0x17a;
  315. break;
  316. case 2:
  317. wk_mask = OMAP3430_ST_UART3_MASK;
  318. padconf = 0x19e;
  319. break;
  320. }
  321. uart->wk_mask = wk_mask;
  322. uart->padconf = padconf;
  323. } else if (cpu_is_omap24xx()) {
  324. u32 wk_mask = 0;
  325. if (cpu_is_omap2430()) {
  326. uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKEN1);
  327. uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKST1);
  328. } else if (cpu_is_omap2420()) {
  329. uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKEN1);
  330. uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKST1);
  331. }
  332. switch (uart->num) {
  333. case 0:
  334. wk_mask = OMAP24XX_ST_UART1_MASK;
  335. break;
  336. case 1:
  337. wk_mask = OMAP24XX_ST_UART2_MASK;
  338. break;
  339. case 2:
  340. wk_mask = OMAP24XX_ST_UART3_MASK;
  341. break;
  342. }
  343. uart->wk_mask = wk_mask;
  344. } else {
  345. uart->wk_en = 0;
  346. uart->wk_st = 0;
  347. uart->wk_mask = 0;
  348. uart->padconf = 0;
  349. }
  350. /* Set wake-enable bit */
  351. if (uart->wk_en && uart->wk_mask) {
  352. v = __raw_readl(uart->wk_en);
  353. v |= uart->wk_mask;
  354. __raw_writel(v, uart->wk_en);
  355. }
  356. /* Ensure IOPAD wake-enables are set */
  357. if (cpu_is_omap34xx() && uart->padconf) {
  358. u16 v;
  359. v = omap_ctrl_readw(uart->padconf);
  360. v |= OMAP3_PADCONF_WAKEUPENABLE0;
  361. omap_ctrl_writew(v, uart->padconf);
  362. }
  363. p->flags |= UPF_SHARE_IRQ;
  364. ret = request_irq(p->irq, omap_uart_interrupt, IRQF_SHARED,
  365. "serial idle", (void *)uart);
  366. WARN_ON(ret);
  367. }
  368. static ssize_t sleep_timeout_show(struct kobject *kobj,
  369. struct kobj_attribute *attr,
  370. char *buf)
  371. {
  372. return sprintf(buf, "%u\n", sleep_timeout / HZ);
  373. }
  374. static ssize_t sleep_timeout_store(struct kobject *kobj,
  375. struct kobj_attribute *attr,
  376. const char *buf, size_t n)
  377. {
  378. struct omap_uart_state *uart;
  379. unsigned int value;
  380. if (sscanf(buf, "%u", &value) != 1) {
  381. printk(KERN_ERR "sleep_timeout_store: Invalid value\n");
  382. return -EINVAL;
  383. }
  384. sleep_timeout = value * HZ;
  385. list_for_each_entry(uart, &uart_list, node) {
  386. uart->timeout = sleep_timeout;
  387. if (uart->timeout)
  388. mod_timer(&uart->timer, jiffies + uart->timeout);
  389. else
  390. /* A zero value means disable timeout feature */
  391. omap_uart_block_sleep(uart);
  392. }
  393. return n;
  394. }
  395. static struct kobj_attribute sleep_timeout_attr =
  396. __ATTR(sleep_timeout, 0644, sleep_timeout_show, sleep_timeout_store);
  397. #else
  398. static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
  399. #endif /* CONFIG_PM */
  400. static struct platform_device serial_device = {
  401. .name = "serial8250",
  402. .id = PLAT8250_DEV_PLATFORM,
  403. .dev = {
  404. .platform_data = serial_platform_data,
  405. },
  406. };
  407. void __init omap_serial_init(void)
  408. {
  409. int i, err;
  410. const struct omap_uart_config *info;
  411. char name[16];
  412. /*
  413. * Make sure the serial ports are muxed on at this point.
  414. * You have to mux them off in device drivers later on
  415. * if not needed.
  416. */
  417. info = omap_get_config(OMAP_TAG_UART, struct omap_uart_config);
  418. if (info == NULL)
  419. return;
  420. if (cpu_is_omap44xx()) {
  421. for (i = 0; i < OMAP_MAX_NR_PORTS; i++)
  422. serial_platform_data[i].irq += 32;
  423. }
  424. for (i = 0; i < OMAP_MAX_NR_PORTS; i++) {
  425. struct plat_serial8250_port *p = serial_platform_data + i;
  426. struct omap_uart_state *uart = &omap_uart[i];
  427. if (!(info->enabled_uarts & (1 << i))) {
  428. p->membase = NULL;
  429. p->mapbase = 0;
  430. continue;
  431. }
  432. sprintf(name, "uart%d_ick", i+1);
  433. uart->ick = clk_get(NULL, name);
  434. if (IS_ERR(uart->ick)) {
  435. printk(KERN_ERR "Could not get uart%d_ick\n", i+1);
  436. uart->ick = NULL;
  437. }
  438. sprintf(name, "uart%d_fck", i+1);
  439. uart->fck = clk_get(NULL, name);
  440. if (IS_ERR(uart->fck)) {
  441. printk(KERN_ERR "Could not get uart%d_fck\n", i+1);
  442. uart->fck = NULL;
  443. }
  444. if (!uart->ick || !uart->fck)
  445. continue;
  446. uart->num = i;
  447. p->private_data = uart;
  448. uart->p = p;
  449. list_add(&uart->node, &uart_list);
  450. omap_uart_enable_clocks(uart);
  451. omap_uart_reset(uart);
  452. omap_uart_idle_init(uart);
  453. }
  454. err = platform_device_register(&serial_device);
  455. #ifdef CONFIG_PM
  456. if (!err)
  457. err = sysfs_create_file(&serial_device.dev.kobj,
  458. &sleep_timeout_attr.attr);
  459. #endif
  460. }