legacy_serial.c 16 KB

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