legacy_serial.c 16 KB

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