legacy_serial.c 17 KB

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