legacy_serial.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. #include <linux/kernel.h>
  2. #include <linux/serial.h>
  3. #include <linux/serial_8250.h>
  4. #include <linux/serial_core.h>
  5. #include <linux/console.h>
  6. #include <linux/pci.h>
  7. #include <linux/of_address.h>
  8. #include <linux/of_device.h>
  9. #include <linux/serial_reg.h>
  10. #include <asm/io.h>
  11. #include <asm/mmu.h>
  12. #include <asm/prom.h>
  13. #include <asm/serial.h>
  14. #include <asm/udbg.h>
  15. #include <asm/pci-bridge.h>
  16. #include <asm/ppc-pci.h>
  17. #undef DEBUG
  18. #ifdef DEBUG
  19. #define DBG(fmt...) do { printk(fmt); } while(0)
  20. #else
  21. #define DBG(fmt...) do { } while(0)
  22. #endif
  23. #define MAX_LEGACY_SERIAL_PORTS 8
  24. static struct plat_serial8250_port
  25. legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
  26. static struct legacy_serial_info {
  27. struct device_node *np;
  28. unsigned int speed;
  29. unsigned int clock;
  30. int irq_check_parent;
  31. phys_addr_t taddr;
  32. } legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
  33. static struct __initdata of_device_id legacy_serial_parents[] = {
  34. {.type = "soc",},
  35. {.type = "tsi-bridge",},
  36. {.type = "opb", },
  37. {.compatible = "ibm,opb",},
  38. {.compatible = "simple-bus",},
  39. {.compatible = "wrs,epld-localbus",},
  40. {},
  41. };
  42. static unsigned int legacy_serial_count;
  43. static int legacy_serial_console = -1;
  44. static unsigned int tsi_serial_in(struct uart_port *p, int offset)
  45. {
  46. unsigned int tmp;
  47. offset = offset << p->regshift;
  48. if (offset == UART_IIR) {
  49. tmp = readl(p->membase + (UART_IIR & ~3));
  50. return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
  51. } else
  52. return readb(p->membase + offset);
  53. }
  54. static void tsi_serial_out(struct uart_port *p, int offset, int value)
  55. {
  56. offset = offset << p->regshift;
  57. if (!((offset == UART_IER) && (value & UART_IER_UUE)))
  58. writeb(value, p->membase + offset);
  59. }
  60. static int __init add_legacy_port(struct device_node *np, int want_index,
  61. int iotype, phys_addr_t base,
  62. phys_addr_t taddr, unsigned long irq,
  63. upf_t flags, int irq_check_parent)
  64. {
  65. const __be32 *clk, *spd;
  66. u32 clock = BASE_BAUD * 16;
  67. int index;
  68. /* get clock freq. if present */
  69. clk = of_get_property(np, "clock-frequency", NULL);
  70. if (clk && *clk)
  71. clock = be32_to_cpup(clk);
  72. /* get default speed if present */
  73. spd = of_get_property(np, "current-speed", NULL);
  74. /* If we have a location index, then try to use it */
  75. if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
  76. index = want_index;
  77. else
  78. index = legacy_serial_count;
  79. /* if our index is still out of range, that mean that
  80. * array is full, we could scan for a free slot but that
  81. * make little sense to bother, just skip the port
  82. */
  83. if (index >= MAX_LEGACY_SERIAL_PORTS)
  84. return -1;
  85. if (index >= legacy_serial_count)
  86. legacy_serial_count = index + 1;
  87. /* Check if there is a port who already claimed our slot */
  88. if (legacy_serial_infos[index].np != 0) {
  89. /* if we still have some room, move it, else override */
  90. if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
  91. printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
  92. index, legacy_serial_count);
  93. legacy_serial_ports[legacy_serial_count] =
  94. legacy_serial_ports[index];
  95. legacy_serial_infos[legacy_serial_count] =
  96. legacy_serial_infos[index];
  97. legacy_serial_count++;
  98. } else {
  99. printk(KERN_DEBUG "Replacing legacy port %d\n", index);
  100. }
  101. }
  102. /* Now fill the entry */
  103. memset(&legacy_serial_ports[index], 0,
  104. sizeof(struct plat_serial8250_port));
  105. if (iotype == UPIO_PORT)
  106. legacy_serial_ports[index].iobase = base;
  107. else
  108. legacy_serial_ports[index].mapbase = base;
  109. legacy_serial_ports[index].iotype = iotype;
  110. legacy_serial_ports[index].uartclk = clock;
  111. legacy_serial_ports[index].irq = irq;
  112. legacy_serial_ports[index].flags = flags;
  113. legacy_serial_infos[index].taddr = taddr;
  114. legacy_serial_infos[index].np = of_node_get(np);
  115. legacy_serial_infos[index].clock = clock;
  116. legacy_serial_infos[index].speed = spd ? be32_to_cpup(spd) : 0;
  117. legacy_serial_infos[index].irq_check_parent = irq_check_parent;
  118. if (iotype == UPIO_TSI) {
  119. legacy_serial_ports[index].serial_in = tsi_serial_in;
  120. legacy_serial_ports[index].serial_out = tsi_serial_out;
  121. }
  122. printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
  123. index, np->full_name);
  124. printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
  125. (iotype == UPIO_PORT) ? "port" : "mem",
  126. (unsigned long long)base, (unsigned long long)taddr, irq,
  127. legacy_serial_ports[index].uartclk,
  128. legacy_serial_infos[index].speed);
  129. return index;
  130. }
  131. static int __init add_legacy_soc_port(struct device_node *np,
  132. struct device_node *soc_dev)
  133. {
  134. u64 addr;
  135. const u32 *addrp;
  136. upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ
  137. | UPF_FIXED_PORT;
  138. struct device_node *tsi = of_get_parent(np);
  139. /* We only support ports that have a clock frequency properly
  140. * encoded in the device-tree.
  141. */
  142. if (of_get_property(np, "clock-frequency", NULL) == NULL)
  143. return -1;
  144. /* if reg-shift or offset, don't try to use it */
  145. if ((of_get_property(np, "reg-shift", NULL) != NULL) ||
  146. (of_get_property(np, "reg-offset", NULL) != NULL))
  147. return -1;
  148. /* if rtas uses this device, don't try to use it as well */
  149. if (of_get_property(np, "used-by-rtas", NULL) != NULL)
  150. return -1;
  151. /* Get the address */
  152. addrp = of_get_address(soc_dev, 0, NULL, NULL);
  153. if (addrp == NULL)
  154. return -1;
  155. addr = of_translate_address(soc_dev, addrp);
  156. if (addr == OF_BAD_ADDR)
  157. return -1;
  158. /* Add port, irq will be dealt with later. We passed a translated
  159. * IO port value. It will be fixed up later along with the irq
  160. */
  161. if (tsi && !strcmp(tsi->type, "tsi-bridge"))
  162. return add_legacy_port(np, -1, UPIO_TSI, addr, addr, NO_IRQ, flags, 0);
  163. else
  164. return add_legacy_port(np, -1, UPIO_MEM, addr, addr, NO_IRQ, flags, 0);
  165. }
  166. static int __init add_legacy_isa_port(struct device_node *np,
  167. struct device_node *isa_brg)
  168. {
  169. const __be32 *reg;
  170. const char *typep;
  171. int index = -1;
  172. u64 taddr;
  173. DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
  174. /* Get the ISA port number */
  175. reg = of_get_property(np, "reg", NULL);
  176. if (reg == NULL)
  177. return -1;
  178. /* Verify it's an IO port, we don't support anything else */
  179. if (!(be32_to_cpu(reg[0]) & 0x00000001))
  180. return -1;
  181. /* Now look for an "ibm,aix-loc" property that gives us ordering
  182. * if any...
  183. */
  184. typep = of_get_property(np, "ibm,aix-loc", NULL);
  185. /* If we have a location index, then use it */
  186. if (typep && *typep == 'S')
  187. index = simple_strtol(typep+1, NULL, 0) - 1;
  188. /* Translate ISA address. If it fails, we still register the port
  189. * with no translated address so that it can be picked up as an IO
  190. * port later by the serial driver
  191. */
  192. taddr = of_translate_address(np, reg);
  193. if (taddr == OF_BAD_ADDR)
  194. taddr = 0;
  195. /* Add port, irq will be dealt with later */
  196. return add_legacy_port(np, index, UPIO_PORT, be32_to_cpu(reg[1]), taddr,
  197. NO_IRQ, UPF_BOOT_AUTOCONF, 0);
  198. }
  199. #ifdef CONFIG_PCI
  200. static int __init add_legacy_pci_port(struct device_node *np,
  201. struct device_node *pci_dev)
  202. {
  203. u64 addr, base;
  204. const u32 *addrp;
  205. unsigned int flags;
  206. int iotype, index = -1, lindex = 0;
  207. DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
  208. /* We only support ports that have a clock frequency properly
  209. * encoded in the device-tree (that is have an fcode). Anything
  210. * else can't be used that early and will be normally probed by
  211. * the generic 8250_pci driver later on. The reason is that 8250
  212. * compatible UARTs on PCI need all sort of quirks (port offsets
  213. * etc...) that this code doesn't know about
  214. */
  215. if (of_get_property(np, "clock-frequency", NULL) == NULL)
  216. return -1;
  217. /* Get the PCI address. Assume BAR 0 */
  218. addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
  219. if (addrp == NULL)
  220. return -1;
  221. /* We only support BAR 0 for now */
  222. iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
  223. addr = of_translate_address(pci_dev, addrp);
  224. if (addr == OF_BAD_ADDR)
  225. return -1;
  226. /* Set the IO base to the same as the translated address for MMIO,
  227. * or to the domain local IO base for PIO (it will be fixed up later)
  228. */
  229. if (iotype == UPIO_MEM)
  230. base = addr;
  231. else
  232. base = addrp[2];
  233. /* Try to guess an index... If we have subdevices of the pci dev,
  234. * we get to their "reg" property
  235. */
  236. if (np != pci_dev) {
  237. const __be32 *reg = of_get_property(np, "reg", NULL);
  238. if (reg && (be32_to_cpup(reg) < 4))
  239. index = lindex = be32_to_cpup(reg);
  240. }
  241. /* Local index means it's the Nth port in the PCI chip. Unfortunately
  242. * the offset to add here is device specific. We know about those
  243. * EXAR ports and we default to the most common case. If your UART
  244. * doesn't work for these settings, you'll have to add your own special
  245. * cases here
  246. */
  247. if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
  248. of_device_is_compatible(pci_dev, "pci13a8,154") ||
  249. of_device_is_compatible(pci_dev, "pci13a8,158")) {
  250. addr += 0x200 * lindex;
  251. base += 0x200 * lindex;
  252. } else {
  253. addr += 8 * lindex;
  254. base += 8 * lindex;
  255. }
  256. /* Add port, irq will be dealt with later. We passed a translated
  257. * IO port value. It will be fixed up later along with the irq
  258. */
  259. return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
  260. UPF_BOOT_AUTOCONF, np != pci_dev);
  261. }
  262. #endif
  263. static void __init setup_legacy_serial_console(int console)
  264. {
  265. struct legacy_serial_info *info =
  266. &legacy_serial_infos[console];
  267. void __iomem *addr;
  268. if (info->taddr == 0)
  269. return;
  270. addr = ioremap(info->taddr, 0x1000);
  271. if (addr == NULL)
  272. return;
  273. if (info->speed == 0)
  274. info->speed = udbg_probe_uart_speed(addr, info->clock);
  275. DBG("default console speed = %d\n", info->speed);
  276. udbg_init_uart(addr, info->speed, info->clock);
  277. }
  278. /*
  279. * This is called very early, as part of setup_system() or eventually
  280. * setup_arch(), basically before anything else in this file. This function
  281. * will try to build a list of all the available 8250-compatible serial ports
  282. * in the machine using the Open Firmware device-tree. It currently only deals
  283. * with ISA and PCI busses but could be extended. It allows a very early boot
  284. * console to be initialized, that list is also used later to provide 8250 with
  285. * the machine non-PCI ports and to properly pick the default console port
  286. */
  287. void __init find_legacy_serial_ports(void)
  288. {
  289. struct device_node *np, *stdout = NULL;
  290. const char *path;
  291. int index;
  292. DBG(" -> find_legacy_serial_port()\n");
  293. /* Now find out if one of these is out firmware console */
  294. path = of_get_property(of_chosen, "linux,stdout-path", NULL);
  295. if (path != NULL) {
  296. stdout = of_find_node_by_path(path);
  297. if (stdout)
  298. DBG("stdout is %s\n", stdout->full_name);
  299. } else {
  300. DBG(" no linux,stdout-path !\n");
  301. }
  302. /* Iterate over all the 16550 ports, looking for known parents */
  303. for_each_compatible_node(np, "serial", "ns16550") {
  304. struct device_node *parent = of_get_parent(np);
  305. if (!parent)
  306. continue;
  307. if (of_match_node(legacy_serial_parents, parent) != NULL) {
  308. if (of_device_is_available(np)) {
  309. index = add_legacy_soc_port(np, np);
  310. if (index >= 0 && np == stdout)
  311. legacy_serial_console = index;
  312. }
  313. }
  314. of_node_put(parent);
  315. }
  316. /* Next, fill our array with ISA ports */
  317. for_each_node_by_type(np, "serial") {
  318. struct device_node *isa = of_get_parent(np);
  319. if (isa && !strcmp(isa->name, "isa")) {
  320. index = add_legacy_isa_port(np, isa);
  321. if (index >= 0 && np == stdout)
  322. legacy_serial_console = index;
  323. }
  324. of_node_put(isa);
  325. }
  326. #ifdef CONFIG_PCI
  327. /* Next, try to locate PCI ports */
  328. for (np = NULL; (np = of_find_all_nodes(np));) {
  329. struct device_node *pci, *parent = of_get_parent(np);
  330. if (parent && !strcmp(parent->name, "isa")) {
  331. of_node_put(parent);
  332. continue;
  333. }
  334. if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
  335. of_node_put(parent);
  336. continue;
  337. }
  338. /* Check for known pciclass, and also check wether we have
  339. * a device with child nodes for ports or not
  340. */
  341. if (of_device_is_compatible(np, "pciclass,0700") ||
  342. of_device_is_compatible(np, "pciclass,070002"))
  343. pci = np;
  344. else if (of_device_is_compatible(parent, "pciclass,0700") ||
  345. of_device_is_compatible(parent, "pciclass,070002"))
  346. pci = parent;
  347. else {
  348. of_node_put(parent);
  349. continue;
  350. }
  351. index = add_legacy_pci_port(np, pci);
  352. if (index >= 0 && np == stdout)
  353. legacy_serial_console = index;
  354. of_node_put(parent);
  355. }
  356. #endif
  357. DBG("legacy_serial_console = %d\n", legacy_serial_console);
  358. if (legacy_serial_console >= 0)
  359. setup_legacy_serial_console(legacy_serial_console);
  360. DBG(" <- find_legacy_serial_port()\n");
  361. }
  362. static struct platform_device serial_device = {
  363. .name = "serial8250",
  364. .id = PLAT8250_DEV_PLATFORM,
  365. .dev = {
  366. .platform_data = legacy_serial_ports,
  367. },
  368. };
  369. static void __init fixup_port_irq(int index,
  370. struct device_node *np,
  371. struct plat_serial8250_port *port)
  372. {
  373. unsigned int virq;
  374. DBG("fixup_port_irq(%d)\n", index);
  375. virq = irq_of_parse_and_map(np, 0);
  376. if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
  377. np = of_get_parent(np);
  378. if (np == NULL)
  379. return;
  380. virq = irq_of_parse_and_map(np, 0);
  381. of_node_put(np);
  382. }
  383. if (virq == NO_IRQ)
  384. return;
  385. port->irq = virq;
  386. #ifdef CONFIG_SERIAL_8250_FSL
  387. if (of_device_is_compatible(np, "fsl,ns16550"))
  388. port->handle_irq = fsl8250_handle_irq;
  389. #endif
  390. }
  391. static void __init fixup_port_pio(int index,
  392. struct device_node *np,
  393. struct plat_serial8250_port *port)
  394. {
  395. #ifdef CONFIG_PCI
  396. struct pci_controller *hose;
  397. DBG("fixup_port_pio(%d)\n", index);
  398. hose = pci_find_hose_for_OF_device(np);
  399. if (hose) {
  400. unsigned long offset = (unsigned long)hose->io_base_virt -
  401. #ifdef CONFIG_PPC64
  402. pci_io_base;
  403. #else
  404. isa_io_base;
  405. #endif
  406. DBG("port %d, IO %lx -> %lx\n",
  407. index, port->iobase, port->iobase + offset);
  408. port->iobase += offset;
  409. }
  410. #endif
  411. }
  412. static void __init fixup_port_mmio(int index,
  413. struct device_node *np,
  414. struct plat_serial8250_port *port)
  415. {
  416. DBG("fixup_port_mmio(%d)\n", index);
  417. port->membase = ioremap(port->mapbase, 0x100);
  418. }
  419. /*
  420. * This is called as an arch initcall, hopefully before the PCI bus is
  421. * probed and/or the 8250 driver loaded since we need to register our
  422. * platform devices before 8250 PCI ones are detected as some of them
  423. * must properly "override" the platform ones.
  424. *
  425. * This function fixes up the interrupt value for platform ports as it
  426. * couldn't be done earlier before interrupt maps have been parsed. It
  427. * also "corrects" the IO address for PIO ports for the same reason,
  428. * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
  429. * registers all those platform ports for use by the 8250 driver when it
  430. * finally loads.
  431. */
  432. static int __init serial_dev_init(void)
  433. {
  434. int i;
  435. if (legacy_serial_count == 0)
  436. return -ENODEV;
  437. /*
  438. * Before we register the platform serial devices, we need
  439. * to fixup their interrupts and their IO ports.
  440. */
  441. DBG("Fixing serial ports interrupts and IO ports ...\n");
  442. for (i = 0; i < legacy_serial_count; i++) {
  443. struct plat_serial8250_port *port = &legacy_serial_ports[i];
  444. struct device_node *np = legacy_serial_infos[i].np;
  445. if (port->irq == NO_IRQ)
  446. fixup_port_irq(i, np, port);
  447. if (port->iotype == UPIO_PORT)
  448. fixup_port_pio(i, np, port);
  449. if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
  450. fixup_port_mmio(i, np, port);
  451. }
  452. DBG("Registering platform serial ports\n");
  453. return platform_device_register(&serial_device);
  454. }
  455. device_initcall(serial_dev_init);
  456. #ifdef CONFIG_SERIAL_8250_CONSOLE
  457. /*
  458. * This is called very early, as part of console_init() (typically just after
  459. * time_init()). This function is respondible for trying to find a good
  460. * default console on serial ports. It tries to match the open firmware
  461. * default output with one of the available serial console drivers that have
  462. * been probed earlier by find_legacy_serial_ports()
  463. */
  464. static int __init check_legacy_serial_console(void)
  465. {
  466. struct device_node *prom_stdout = NULL;
  467. int i, speed = 0, offset = 0;
  468. const char *name;
  469. const __be32 *spd;
  470. DBG(" -> check_legacy_serial_console()\n");
  471. /* The user has requested a console so this is already set up. */
  472. if (strstr(boot_command_line, "console=")) {
  473. DBG(" console was specified !\n");
  474. return -EBUSY;
  475. }
  476. if (!of_chosen) {
  477. DBG(" of_chosen is NULL !\n");
  478. return -ENODEV;
  479. }
  480. if (legacy_serial_console < 0) {
  481. DBG(" legacy_serial_console not found !\n");
  482. return -ENODEV;
  483. }
  484. /* We are getting a weird phandle from OF ... */
  485. /* ... So use the full path instead */
  486. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  487. if (name == NULL) {
  488. DBG(" no linux,stdout-path !\n");
  489. return -ENODEV;
  490. }
  491. prom_stdout = of_find_node_by_path(name);
  492. if (!prom_stdout) {
  493. DBG(" can't find stdout package %s !\n", name);
  494. return -ENODEV;
  495. }
  496. DBG("stdout is %s\n", prom_stdout->full_name);
  497. name = of_get_property(prom_stdout, "name", NULL);
  498. if (!name) {
  499. DBG(" stdout package has no name !\n");
  500. goto not_found;
  501. }
  502. spd = of_get_property(prom_stdout, "current-speed", NULL);
  503. if (spd)
  504. speed = be32_to_cpup(spd);
  505. if (strcmp(name, "serial") != 0)
  506. goto not_found;
  507. /* Look for it in probed array */
  508. for (i = 0; i < legacy_serial_count; i++) {
  509. if (prom_stdout != legacy_serial_infos[i].np)
  510. continue;
  511. offset = i;
  512. speed = legacy_serial_infos[i].speed;
  513. break;
  514. }
  515. if (i >= legacy_serial_count)
  516. goto not_found;
  517. of_node_put(prom_stdout);
  518. DBG("Found serial console at ttyS%d\n", offset);
  519. if (speed) {
  520. static char __initdata opt[16];
  521. sprintf(opt, "%d", speed);
  522. return add_preferred_console("ttyS", offset, opt);
  523. } else
  524. return add_preferred_console("ttyS", offset, NULL);
  525. not_found:
  526. DBG("No preferred console found !\n");
  527. of_node_put(prom_stdout);
  528. return -ENODEV;
  529. }
  530. console_initcall(check_legacy_serial_console);
  531. #endif /* CONFIG_SERIAL_8250_CONSOLE */