legacy_serial.c 17 KB

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