legacy_serial.c 16 KB

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