early_printk.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. #include <linux/console.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/string.h>
  5. #include <linux/screen_info.h>
  6. #include <linux/usb/ch9.h>
  7. #include <linux/pci_regs.h>
  8. #include <linux/pci_ids.h>
  9. #include <linux/errno.h>
  10. #include <asm/io.h>
  11. #include <asm/processor.h>
  12. #include <asm/fcntl.h>
  13. #include <asm/setup.h>
  14. #include <xen/hvc-console.h>
  15. #include <asm/pci-direct.h>
  16. #include <asm/fixmap.h>
  17. #include <asm/pgtable.h>
  18. #include <linux/usb/ehci_def.h>
  19. /* Simple VGA output */
  20. #define VGABASE (__ISA_IO_base + 0xb8000)
  21. static int max_ypos = 25, max_xpos = 80;
  22. static int current_ypos = 25, current_xpos;
  23. static void early_vga_write(struct console *con, const char *str, unsigned n)
  24. {
  25. char c;
  26. int i, k, j;
  27. while ((c = *str++) != '\0' && n-- > 0) {
  28. if (current_ypos >= max_ypos) {
  29. /* scroll 1 line up */
  30. for (k = 1, j = 0; k < max_ypos; k++, j++) {
  31. for (i = 0; i < max_xpos; i++) {
  32. writew(readw(VGABASE+2*(max_xpos*k+i)),
  33. VGABASE + 2*(max_xpos*j + i));
  34. }
  35. }
  36. for (i = 0; i < max_xpos; i++)
  37. writew(0x720, VGABASE + 2*(max_xpos*j + i));
  38. current_ypos = max_ypos-1;
  39. }
  40. if (c == '\n') {
  41. current_xpos = 0;
  42. current_ypos++;
  43. } else if (c != '\r') {
  44. writew(((0x7 << 8) | (unsigned short) c),
  45. VGABASE + 2*(max_xpos*current_ypos +
  46. current_xpos++));
  47. if (current_xpos >= max_xpos) {
  48. current_xpos = 0;
  49. current_ypos++;
  50. }
  51. }
  52. }
  53. }
  54. static struct console early_vga_console = {
  55. .name = "earlyvga",
  56. .write = early_vga_write,
  57. .flags = CON_PRINTBUFFER,
  58. .index = -1,
  59. };
  60. /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
  61. static int early_serial_base = 0x3f8; /* ttyS0 */
  62. #define XMTRDY 0x20
  63. #define DLAB 0x80
  64. #define TXR 0 /* Transmit register (WRITE) */
  65. #define RXR 0 /* Receive register (READ) */
  66. #define IER 1 /* Interrupt Enable */
  67. #define IIR 2 /* Interrupt ID */
  68. #define FCR 2 /* FIFO control */
  69. #define LCR 3 /* Line control */
  70. #define MCR 4 /* Modem control */
  71. #define LSR 5 /* Line Status */
  72. #define MSR 6 /* Modem Status */
  73. #define DLL 0 /* Divisor Latch Low */
  74. #define DLH 1 /* Divisor latch High */
  75. static int early_serial_putc(unsigned char ch)
  76. {
  77. unsigned timeout = 0xffff;
  78. while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
  79. cpu_relax();
  80. outb(ch, early_serial_base + TXR);
  81. return timeout ? 0 : -1;
  82. }
  83. static void early_serial_write(struct console *con, const char *s, unsigned n)
  84. {
  85. while (*s && n-- > 0) {
  86. if (*s == '\n')
  87. early_serial_putc('\r');
  88. early_serial_putc(*s);
  89. s++;
  90. }
  91. }
  92. #define DEFAULT_BAUD 9600
  93. static __init void early_serial_init(char *s)
  94. {
  95. unsigned char c;
  96. unsigned divisor;
  97. unsigned baud = DEFAULT_BAUD;
  98. char *e;
  99. if (*s == ',')
  100. ++s;
  101. if (*s) {
  102. unsigned port;
  103. if (!strncmp(s, "0x", 2)) {
  104. early_serial_base = simple_strtoul(s, &e, 16);
  105. } else {
  106. static const int __initconst bases[] = { 0x3f8, 0x2f8 };
  107. if (!strncmp(s, "ttyS", 4))
  108. s += 4;
  109. port = simple_strtoul(s, &e, 10);
  110. if (port > 1 || s == e)
  111. port = 0;
  112. early_serial_base = bases[port];
  113. }
  114. s += strcspn(s, ",");
  115. if (*s == ',')
  116. s++;
  117. }
  118. outb(0x3, early_serial_base + LCR); /* 8n1 */
  119. outb(0, early_serial_base + IER); /* no interrupt */
  120. outb(0, early_serial_base + FCR); /* no fifo */
  121. outb(0x3, early_serial_base + MCR); /* DTR + RTS */
  122. if (*s) {
  123. baud = simple_strtoul(s, &e, 0);
  124. if (baud == 0 || s == e)
  125. baud = DEFAULT_BAUD;
  126. }
  127. divisor = 115200 / baud;
  128. c = inb(early_serial_base + LCR);
  129. outb(c | DLAB, early_serial_base + LCR);
  130. outb(divisor & 0xff, early_serial_base + DLL);
  131. outb((divisor >> 8) & 0xff, early_serial_base + DLH);
  132. outb(c & ~DLAB, early_serial_base + LCR);
  133. }
  134. static struct console early_serial_console = {
  135. .name = "earlyser",
  136. .write = early_serial_write,
  137. .flags = CON_PRINTBUFFER,
  138. .index = -1,
  139. };
  140. #ifdef CONFIG_EARLY_PRINTK_DBGP
  141. static struct ehci_caps __iomem *ehci_caps;
  142. static struct ehci_regs __iomem *ehci_regs;
  143. static struct ehci_dbg_port __iomem *ehci_debug;
  144. static unsigned int dbgp_endpoint_out;
  145. struct ehci_dev {
  146. u32 bus;
  147. u32 slot;
  148. u32 func;
  149. };
  150. static struct ehci_dev ehci_dev;
  151. #define USB_DEBUG_DEVNUM 127
  152. #define DBGP_DATA_TOGGLE 0x8800
  153. static inline u32 dbgp_pid_update(u32 x, u32 tok)
  154. {
  155. return ((x ^ DBGP_DATA_TOGGLE) & 0xffff00) | (tok & 0xff);
  156. }
  157. static inline u32 dbgp_len_update(u32 x, u32 len)
  158. {
  159. return (x & ~0x0f) | (len & 0x0f);
  160. }
  161. /*
  162. * USB Packet IDs (PIDs)
  163. */
  164. /* token */
  165. #define USB_PID_OUT 0xe1
  166. #define USB_PID_IN 0x69
  167. #define USB_PID_SOF 0xa5
  168. #define USB_PID_SETUP 0x2d
  169. /* handshake */
  170. #define USB_PID_ACK 0xd2
  171. #define USB_PID_NAK 0x5a
  172. #define USB_PID_STALL 0x1e
  173. #define USB_PID_NYET 0x96
  174. /* data */
  175. #define USB_PID_DATA0 0xc3
  176. #define USB_PID_DATA1 0x4b
  177. #define USB_PID_DATA2 0x87
  178. #define USB_PID_MDATA 0x0f
  179. /* Special */
  180. #define USB_PID_PREAMBLE 0x3c
  181. #define USB_PID_ERR 0x3c
  182. #define USB_PID_SPLIT 0x78
  183. #define USB_PID_PING 0xb4
  184. #define USB_PID_UNDEF_0 0xf0
  185. #define USB_PID_DATA_TOGGLE 0x88
  186. #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
  187. #define PCI_CAP_ID_EHCI_DEBUG 0xa
  188. #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
  189. #define HUB_SHORT_RESET_TIME 10
  190. #define HUB_LONG_RESET_TIME 200
  191. #define HUB_RESET_TIMEOUT 500
  192. #define DBGP_MAX_PACKET 8
  193. static int dbgp_wait_until_complete(void)
  194. {
  195. u32 ctrl;
  196. int loop = 0x100000;
  197. do {
  198. ctrl = readl(&ehci_debug->control);
  199. /* Stop when the transaction is finished */
  200. if (ctrl & DBGP_DONE)
  201. break;
  202. } while (--loop > 0);
  203. if (!loop)
  204. return -1;
  205. /*
  206. * Now that we have observed the completed transaction,
  207. * clear the done bit.
  208. */
  209. writel(ctrl | DBGP_DONE, &ehci_debug->control);
  210. return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
  211. }
  212. static void __init dbgp_mdelay(int ms)
  213. {
  214. int i;
  215. while (ms--) {
  216. for (i = 0; i < 1000; i++)
  217. outb(0x1, 0x80);
  218. }
  219. }
  220. static void dbgp_breath(void)
  221. {
  222. /* Sleep to give the debug port a chance to breathe */
  223. }
  224. static int dbgp_wait_until_done(unsigned ctrl)
  225. {
  226. u32 pids, lpid;
  227. int ret;
  228. int loop = 3;
  229. retry:
  230. writel(ctrl | DBGP_GO, &ehci_debug->control);
  231. ret = dbgp_wait_until_complete();
  232. pids = readl(&ehci_debug->pids);
  233. lpid = DBGP_PID_GET(pids);
  234. if (ret < 0)
  235. return ret;
  236. /*
  237. * If the port is getting full or it has dropped data
  238. * start pacing ourselves, not necessary but it's friendly.
  239. */
  240. if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
  241. dbgp_breath();
  242. /* If I get a NACK reissue the transmission */
  243. if (lpid == USB_PID_NAK) {
  244. if (--loop > 0)
  245. goto retry;
  246. }
  247. return ret;
  248. }
  249. static void dbgp_set_data(const void *buf, int size)
  250. {
  251. const unsigned char *bytes = buf;
  252. u32 lo, hi;
  253. int i;
  254. lo = hi = 0;
  255. for (i = 0; i < 4 && i < size; i++)
  256. lo |= bytes[i] << (8*i);
  257. for (; i < 8 && i < size; i++)
  258. hi |= bytes[i] << (8*(i - 4));
  259. writel(lo, &ehci_debug->data03);
  260. writel(hi, &ehci_debug->data47);
  261. }
  262. static void __init dbgp_get_data(void *buf, int size)
  263. {
  264. unsigned char *bytes = buf;
  265. u32 lo, hi;
  266. int i;
  267. lo = readl(&ehci_debug->data03);
  268. hi = readl(&ehci_debug->data47);
  269. for (i = 0; i < 4 && i < size; i++)
  270. bytes[i] = (lo >> (8*i)) & 0xff;
  271. for (; i < 8 && i < size; i++)
  272. bytes[i] = (hi >> (8*(i - 4))) & 0xff;
  273. }
  274. static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
  275. const char *bytes, int size)
  276. {
  277. u32 pids, addr, ctrl;
  278. int ret;
  279. if (size > DBGP_MAX_PACKET)
  280. return -1;
  281. addr = DBGP_EPADDR(devnum, endpoint);
  282. pids = readl(&ehci_debug->pids);
  283. pids = dbgp_pid_update(pids, USB_PID_OUT);
  284. ctrl = readl(&ehci_debug->control);
  285. ctrl = dbgp_len_update(ctrl, size);
  286. ctrl |= DBGP_OUT;
  287. ctrl |= DBGP_GO;
  288. dbgp_set_data(bytes, size);
  289. writel(addr, &ehci_debug->address);
  290. writel(pids, &ehci_debug->pids);
  291. ret = dbgp_wait_until_done(ctrl);
  292. if (ret < 0)
  293. return ret;
  294. return ret;
  295. }
  296. static int __init dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
  297. int size)
  298. {
  299. u32 pids, addr, ctrl;
  300. int ret;
  301. if (size > DBGP_MAX_PACKET)
  302. return -1;
  303. addr = DBGP_EPADDR(devnum, endpoint);
  304. pids = readl(&ehci_debug->pids);
  305. pids = dbgp_pid_update(pids, USB_PID_IN);
  306. ctrl = readl(&ehci_debug->control);
  307. ctrl = dbgp_len_update(ctrl, size);
  308. ctrl &= ~DBGP_OUT;
  309. ctrl |= DBGP_GO;
  310. writel(addr, &ehci_debug->address);
  311. writel(pids, &ehci_debug->pids);
  312. ret = dbgp_wait_until_done(ctrl);
  313. if (ret < 0)
  314. return ret;
  315. if (size > ret)
  316. size = ret;
  317. dbgp_get_data(data, size);
  318. return ret;
  319. }
  320. static int __init dbgp_control_msg(unsigned devnum, int requesttype,
  321. int request, int value, int index, void *data, int size)
  322. {
  323. u32 pids, addr, ctrl;
  324. struct usb_ctrlrequest req;
  325. int read;
  326. int ret;
  327. read = (requesttype & USB_DIR_IN) != 0;
  328. if (size > (read ? DBGP_MAX_PACKET:0))
  329. return -1;
  330. /* Compute the control message */
  331. req.bRequestType = requesttype;
  332. req.bRequest = request;
  333. req.wValue = cpu_to_le16(value);
  334. req.wIndex = cpu_to_le16(index);
  335. req.wLength = cpu_to_le16(size);
  336. pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
  337. addr = DBGP_EPADDR(devnum, 0);
  338. ctrl = readl(&ehci_debug->control);
  339. ctrl = dbgp_len_update(ctrl, sizeof(req));
  340. ctrl |= DBGP_OUT;
  341. ctrl |= DBGP_GO;
  342. /* Send the setup message */
  343. dbgp_set_data(&req, sizeof(req));
  344. writel(addr, &ehci_debug->address);
  345. writel(pids, &ehci_debug->pids);
  346. ret = dbgp_wait_until_done(ctrl);
  347. if (ret < 0)
  348. return ret;
  349. /* Read the result */
  350. return dbgp_bulk_read(devnum, 0, data, size);
  351. }
  352. /* Find a PCI capability */
  353. static u32 __init find_cap(u32 num, u32 slot, u32 func, int cap)
  354. {
  355. u8 pos;
  356. int bytes;
  357. if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
  358. PCI_STATUS_CAP_LIST))
  359. return 0;
  360. pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
  361. for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
  362. u8 id;
  363. pos &= ~3;
  364. id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
  365. if (id == 0xff)
  366. break;
  367. if (id == cap)
  368. return pos;
  369. pos = read_pci_config_byte(num, slot, func,
  370. pos+PCI_CAP_LIST_NEXT);
  371. }
  372. return 0;
  373. }
  374. static u32 __init __find_dbgp(u32 bus, u32 slot, u32 func)
  375. {
  376. u32 class;
  377. class = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
  378. if ((class >> 8) != PCI_CLASS_SERIAL_USB_EHCI)
  379. return 0;
  380. return find_cap(bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
  381. }
  382. static u32 __init find_dbgp(int ehci_num, u32 *rbus, u32 *rslot, u32 *rfunc)
  383. {
  384. u32 bus, slot, func;
  385. for (bus = 0; bus < 256; bus++) {
  386. for (slot = 0; slot < 32; slot++) {
  387. for (func = 0; func < 8; func++) {
  388. unsigned cap;
  389. cap = __find_dbgp(bus, slot, func);
  390. if (!cap)
  391. continue;
  392. if (ehci_num-- != 0)
  393. continue;
  394. *rbus = bus;
  395. *rslot = slot;
  396. *rfunc = func;
  397. return cap;
  398. }
  399. }
  400. }
  401. return 0;
  402. }
  403. static int __init ehci_reset_port(int port)
  404. {
  405. u32 portsc;
  406. u32 delay_time, delay;
  407. int loop;
  408. /* Reset the usb debug port */
  409. portsc = readl(&ehci_regs->port_status[port - 1]);
  410. portsc &= ~PORT_PE;
  411. portsc |= PORT_RESET;
  412. writel(portsc, &ehci_regs->port_status[port - 1]);
  413. delay = HUB_ROOT_RESET_TIME;
  414. for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
  415. delay_time += delay) {
  416. dbgp_mdelay(delay);
  417. portsc = readl(&ehci_regs->port_status[port - 1]);
  418. if (portsc & PORT_RESET) {
  419. /* force reset to complete */
  420. loop = 2;
  421. writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
  422. &ehci_regs->port_status[port - 1]);
  423. do {
  424. portsc = readl(&ehci_regs->port_status[port-1]);
  425. } while ((portsc & PORT_RESET) && (--loop > 0));
  426. }
  427. /* Device went away? */
  428. if (!(portsc & PORT_CONNECT))
  429. return -ENOTCONN;
  430. /* bomb out completely if something weird happend */
  431. if ((portsc & PORT_CSC))
  432. return -EINVAL;
  433. /* If we've finished resetting, then break out of the loop */
  434. if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
  435. return 0;
  436. }
  437. return -EBUSY;
  438. }
  439. static int __init ehci_wait_for_port(int port)
  440. {
  441. u32 status;
  442. int ret, reps;
  443. for (reps = 0; reps < 3; reps++) {
  444. dbgp_mdelay(100);
  445. status = readl(&ehci_regs->status);
  446. if (status & STS_PCD) {
  447. ret = ehci_reset_port(port);
  448. if (ret == 0)
  449. return 0;
  450. }
  451. }
  452. return -ENOTCONN;
  453. }
  454. #ifdef DBGP_DEBUG
  455. # define dbgp_printk early_printk
  456. #else
  457. static inline void dbgp_printk(const char *fmt, ...) { }
  458. #endif
  459. typedef void (*set_debug_port_t)(int port);
  460. static void __init default_set_debug_port(int port)
  461. {
  462. }
  463. static set_debug_port_t __initdata set_debug_port = default_set_debug_port;
  464. static void __init nvidia_set_debug_port(int port)
  465. {
  466. u32 dword;
  467. dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  468. 0x74);
  469. dword &= ~(0x0f<<12);
  470. dword |= ((port & 0x0f)<<12);
  471. write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
  472. dword);
  473. dbgp_printk("set debug port to %d\n", port);
  474. }
  475. static void __init detect_set_debug_port(void)
  476. {
  477. u32 vendorid;
  478. vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  479. 0x00);
  480. if ((vendorid & 0xffff) == 0x10de) {
  481. dbgp_printk("using nvidia set_debug_port\n");
  482. set_debug_port = nvidia_set_debug_port;
  483. }
  484. }
  485. static int __init ehci_setup(void)
  486. {
  487. struct usb_debug_descriptor dbgp_desc;
  488. u32 cmd, ctrl, status, portsc, hcs_params;
  489. u32 debug_port, new_debug_port = 0, n_ports;
  490. u32 devnum;
  491. int ret, i;
  492. int loop;
  493. int port_map_tried;
  494. int playtimes = 3;
  495. try_next_time:
  496. port_map_tried = 0;
  497. try_next_port:
  498. hcs_params = readl(&ehci_caps->hcs_params);
  499. debug_port = HCS_DEBUG_PORT(hcs_params);
  500. n_ports = HCS_N_PORTS(hcs_params);
  501. dbgp_printk("debug_port: %d\n", debug_port);
  502. dbgp_printk("n_ports: %d\n", n_ports);
  503. for (i = 1; i <= n_ports; i++) {
  504. portsc = readl(&ehci_regs->port_status[i-1]);
  505. dbgp_printk("portstatus%d: %08x\n", i, portsc);
  506. }
  507. if (port_map_tried && (new_debug_port != debug_port)) {
  508. if (--playtimes) {
  509. set_debug_port(new_debug_port);
  510. goto try_next_time;
  511. }
  512. return -1;
  513. }
  514. loop = 10;
  515. /* Reset the EHCI controller */
  516. cmd = readl(&ehci_regs->command);
  517. cmd |= CMD_RESET;
  518. writel(cmd, &ehci_regs->command);
  519. do {
  520. cmd = readl(&ehci_regs->command);
  521. } while ((cmd & CMD_RESET) && (--loop > 0));
  522. if (!loop) {
  523. dbgp_printk("can not reset ehci\n");
  524. return -1;
  525. }
  526. dbgp_printk("ehci reset done\n");
  527. /* Claim ownership, but do not enable yet */
  528. ctrl = readl(&ehci_debug->control);
  529. ctrl |= DBGP_OWNER;
  530. ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
  531. writel(ctrl, &ehci_debug->control);
  532. /* Start the ehci running */
  533. cmd = readl(&ehci_regs->command);
  534. cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
  535. cmd |= CMD_RUN;
  536. writel(cmd, &ehci_regs->command);
  537. /* Ensure everything is routed to the EHCI */
  538. writel(FLAG_CF, &ehci_regs->configured_flag);
  539. /* Wait until the controller is no longer halted */
  540. loop = 10;
  541. do {
  542. status = readl(&ehci_regs->status);
  543. } while ((status & STS_HALT) && (--loop > 0));
  544. if (!loop) {
  545. dbgp_printk("ehci can be started\n");
  546. return -1;
  547. }
  548. dbgp_printk("ehci started\n");
  549. /* Wait for a device to show up in the debug port */
  550. ret = ehci_wait_for_port(debug_port);
  551. if (ret < 0) {
  552. dbgp_printk("No device found in debug port\n");
  553. goto next_debug_port;
  554. }
  555. dbgp_printk("ehci wait for port done\n");
  556. /* Enable the debug port */
  557. ctrl = readl(&ehci_debug->control);
  558. ctrl |= DBGP_CLAIM;
  559. writel(ctrl, &ehci_debug->control);
  560. ctrl = readl(&ehci_debug->control);
  561. if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
  562. dbgp_printk("No device in debug port\n");
  563. writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
  564. goto err;
  565. }
  566. dbgp_printk("debug ported enabled\n");
  567. /* Completely transfer the debug device to the debug controller */
  568. portsc = readl(&ehci_regs->port_status[debug_port - 1]);
  569. portsc &= ~PORT_PE;
  570. writel(portsc, &ehci_regs->port_status[debug_port - 1]);
  571. dbgp_mdelay(100);
  572. /* Find the debug device and make it device number 127 */
  573. for (devnum = 0; devnum <= 127; devnum++) {
  574. ret = dbgp_control_msg(devnum,
  575. USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  576. USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
  577. &dbgp_desc, sizeof(dbgp_desc));
  578. if (ret > 0)
  579. break;
  580. }
  581. if (devnum > 127) {
  582. dbgp_printk("Could not find attached debug device\n");
  583. goto err;
  584. }
  585. if (ret < 0) {
  586. dbgp_printk("Attached device is not a debug device\n");
  587. goto err;
  588. }
  589. dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
  590. /* Move the device to 127 if it isn't already there */
  591. if (devnum != USB_DEBUG_DEVNUM) {
  592. ret = dbgp_control_msg(devnum,
  593. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  594. USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
  595. if (ret < 0) {
  596. dbgp_printk("Could not move attached device to %d\n",
  597. USB_DEBUG_DEVNUM);
  598. goto err;
  599. }
  600. devnum = USB_DEBUG_DEVNUM;
  601. dbgp_printk("debug device renamed to 127\n");
  602. }
  603. /* Enable the debug interface */
  604. ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
  605. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  606. USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
  607. if (ret < 0) {
  608. dbgp_printk(" Could not enable the debug device\n");
  609. goto err;
  610. }
  611. dbgp_printk("debug interface enabled\n");
  612. /* Perform a small write to get the even/odd data state in sync
  613. */
  614. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
  615. if (ret < 0) {
  616. dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
  617. goto err;
  618. }
  619. dbgp_printk("small write doned\n");
  620. return 0;
  621. err:
  622. /* Things didn't work so remove my claim */
  623. ctrl = readl(&ehci_debug->control);
  624. ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
  625. writel(ctrl, &ehci_debug->control);
  626. return -1;
  627. next_debug_port:
  628. port_map_tried |= (1<<(debug_port - 1));
  629. new_debug_port = ((debug_port-1+1)%n_ports) + 1;
  630. if (port_map_tried != ((1<<n_ports) - 1)) {
  631. set_debug_port(new_debug_port);
  632. goto try_next_port;
  633. }
  634. if (--playtimes) {
  635. set_debug_port(new_debug_port);
  636. goto try_next_time;
  637. }
  638. return -1;
  639. }
  640. static int __init early_dbgp_init(char *s)
  641. {
  642. u32 debug_port, bar, offset;
  643. u32 bus, slot, func, cap;
  644. void __iomem *ehci_bar;
  645. u32 dbgp_num;
  646. u32 bar_val;
  647. char *e;
  648. int ret;
  649. u8 byte;
  650. if (!early_pci_allowed())
  651. return -1;
  652. dbgp_num = 0;
  653. if (*s)
  654. dbgp_num = simple_strtoul(s, &e, 10);
  655. dbgp_printk("dbgp_num: %d\n", dbgp_num);
  656. cap = find_dbgp(dbgp_num, &bus, &slot, &func);
  657. if (!cap)
  658. return -1;
  659. dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
  660. func);
  661. debug_port = read_pci_config(bus, slot, func, cap);
  662. bar = (debug_port >> 29) & 0x7;
  663. bar = (bar * 4) + 0xc;
  664. offset = (debug_port >> 16) & 0xfff;
  665. dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
  666. if (bar != PCI_BASE_ADDRESS_0) {
  667. dbgp_printk("only debug ports on bar 1 handled.\n");
  668. return -1;
  669. }
  670. bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
  671. dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
  672. if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
  673. dbgp_printk("only simple 32bit mmio bars supported\n");
  674. return -1;
  675. }
  676. /* double check if the mem space is enabled */
  677. byte = read_pci_config_byte(bus, slot, func, 0x04);
  678. if (!(byte & 0x2)) {
  679. byte |= 0x02;
  680. write_pci_config_byte(bus, slot, func, 0x04, byte);
  681. dbgp_printk("mmio for ehci enabled\n");
  682. }
  683. /*
  684. * FIXME I don't have the bar size so just guess PAGE_SIZE is more
  685. * than enough. 1K is the biggest I have seen.
  686. */
  687. set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
  688. ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
  689. ehci_bar += bar_val & ~PAGE_MASK;
  690. dbgp_printk("ehci_bar: %p\n", ehci_bar);
  691. ehci_caps = ehci_bar;
  692. ehci_regs = ehci_bar + HC_LENGTH(readl(&ehci_caps->hc_capbase));
  693. ehci_debug = ehci_bar + offset;
  694. ehci_dev.bus = bus;
  695. ehci_dev.slot = slot;
  696. ehci_dev.func = func;
  697. detect_set_debug_port();
  698. ret = ehci_setup();
  699. if (ret < 0) {
  700. dbgp_printk("ehci_setup failed\n");
  701. ehci_debug = NULL;
  702. return -1;
  703. }
  704. return 0;
  705. }
  706. static void early_dbgp_write(struct console *con, const char *str, u32 n)
  707. {
  708. int chunk, ret;
  709. if (!ehci_debug)
  710. return;
  711. while (n > 0) {
  712. chunk = n;
  713. if (chunk > DBGP_MAX_PACKET)
  714. chunk = DBGP_MAX_PACKET;
  715. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
  716. dbgp_endpoint_out, str, chunk);
  717. str += chunk;
  718. n -= chunk;
  719. }
  720. }
  721. static struct console early_dbgp_console = {
  722. .name = "earlydbg",
  723. .write = early_dbgp_write,
  724. .flags = CON_PRINTBUFFER,
  725. .index = -1,
  726. };
  727. #endif
  728. /* Direct interface for emergencies */
  729. static struct console *early_console = &early_vga_console;
  730. static int __initdata early_console_initialized;
  731. asmlinkage void early_printk(const char *fmt, ...)
  732. {
  733. char buf[512];
  734. int n;
  735. va_list ap;
  736. va_start(ap, fmt);
  737. n = vscnprintf(buf, sizeof(buf), fmt, ap);
  738. early_console->write(early_console, buf, n);
  739. va_end(ap);
  740. }
  741. static int __init setup_early_printk(char *buf)
  742. {
  743. int keep_early;
  744. if (!buf)
  745. return 0;
  746. if (early_console_initialized)
  747. return 0;
  748. early_console_initialized = 1;
  749. keep_early = (strstr(buf, "keep") != NULL);
  750. if (!strncmp(buf, "serial", 6)) {
  751. early_serial_init(buf + 6);
  752. early_console = &early_serial_console;
  753. } else if (!strncmp(buf, "ttyS", 4)) {
  754. early_serial_init(buf);
  755. early_console = &early_serial_console;
  756. } else if (!strncmp(buf, "vga", 3)
  757. && boot_params.screen_info.orig_video_isVGA == 1) {
  758. max_xpos = boot_params.screen_info.orig_video_cols;
  759. max_ypos = boot_params.screen_info.orig_video_lines;
  760. current_ypos = boot_params.screen_info.orig_y;
  761. early_console = &early_vga_console;
  762. #ifdef CONFIG_EARLY_PRINTK_DBGP
  763. } else if (!strncmp(buf, "dbgp", 4)) {
  764. if (early_dbgp_init(buf+4) < 0)
  765. return 0;
  766. early_console = &early_dbgp_console;
  767. /*
  768. * usb subsys will reset ehci controller, so don't keep
  769. * that early console
  770. */
  771. keep_early = 0;
  772. #endif
  773. #ifdef CONFIG_HVC_XEN
  774. } else if (!strncmp(buf, "xen", 3)) {
  775. early_console = &xenboot_console;
  776. #endif
  777. }
  778. if (keep_early)
  779. early_console->flags &= ~CON_BOOT;
  780. else
  781. early_console->flags |= CON_BOOT;
  782. register_console(early_console);
  783. return 0;
  784. }
  785. early_param("earlyprintk", setup_early_printk);