legacy_serial.c 15 KB

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