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