legacy_serial.c 17 KB

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