ip22-gio.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #include <linux/export.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/slab.h>
  5. #include <asm/addrspace.h>
  6. #include <asm/paccess.h>
  7. #include <asm/gio_device.h>
  8. #include <asm/sgi/gio.h>
  9. #include <asm/sgi/hpc3.h>
  10. #include <asm/sgi/mc.h>
  11. #include <asm/sgi/ip22.h>
  12. static struct bus_type gio_bus_type;
  13. static struct {
  14. const char *name;
  15. __u8 id;
  16. } gio_name_table[] = {
  17. { .name = "SGI Impact", .id = 0x10 },
  18. { .name = "Phobos G160", .id = 0x35 },
  19. { .name = "Phobos G130", .id = 0x36 },
  20. { .name = "Phobos G100", .id = 0x37 },
  21. { .name = "Set Engineering GFE", .id = 0x38 },
  22. /* fake IDs */
  23. { .name = "SGI Newport", .id = 0x7e },
  24. { .name = "SGI GR2/GR3", .id = 0x7f },
  25. };
  26. static struct device gio_bus = {
  27. .init_name = "gio",
  28. };
  29. /**
  30. * gio_match_device - Tell if an of_device structure has a matching
  31. * gio_match structure
  32. * @ids: array of of device match structures to search in
  33. * @dev: the of device structure to match against
  34. *
  35. * Used by a driver to check whether an of_device present in the
  36. * system is in its list of supported devices.
  37. */
  38. const struct gio_device_id *gio_match_device(const struct gio_device_id *match,
  39. const struct gio_device *dev)
  40. {
  41. const struct gio_device_id *ids;
  42. for (ids = match; ids->id != 0xff; ids++)
  43. if (ids->id == dev->id.id)
  44. return ids;
  45. return NULL;
  46. }
  47. EXPORT_SYMBOL_GPL(gio_match_device);
  48. struct gio_device *gio_dev_get(struct gio_device *dev)
  49. {
  50. struct device *tmp;
  51. if (!dev)
  52. return NULL;
  53. tmp = get_device(&dev->dev);
  54. if (tmp)
  55. return to_gio_device(tmp);
  56. else
  57. return NULL;
  58. }
  59. EXPORT_SYMBOL_GPL(gio_dev_get);
  60. void gio_dev_put(struct gio_device *dev)
  61. {
  62. if (dev)
  63. put_device(&dev->dev);
  64. }
  65. EXPORT_SYMBOL_GPL(gio_dev_put);
  66. /**
  67. * gio_release_dev - free an gio device structure when all users of it are finished.
  68. * @dev: device that's been disconnected
  69. *
  70. * Will be called only by the device core when all users of this gio device are
  71. * done.
  72. */
  73. void gio_release_dev(struct device *dev)
  74. {
  75. struct gio_device *giodev;
  76. giodev = to_gio_device(dev);
  77. kfree(giodev);
  78. }
  79. EXPORT_SYMBOL_GPL(gio_release_dev);
  80. int gio_device_register(struct gio_device *giodev)
  81. {
  82. giodev->dev.bus = &gio_bus_type;
  83. giodev->dev.parent = &gio_bus;
  84. return device_register(&giodev->dev);
  85. }
  86. EXPORT_SYMBOL_GPL(gio_device_register);
  87. void gio_device_unregister(struct gio_device *giodev)
  88. {
  89. device_unregister(&giodev->dev);
  90. }
  91. EXPORT_SYMBOL_GPL(gio_device_unregister);
  92. static int gio_bus_match(struct device *dev, struct device_driver *drv)
  93. {
  94. struct gio_device *gio_dev = to_gio_device(dev);
  95. struct gio_driver *gio_drv = to_gio_driver(drv);
  96. return gio_match_device(gio_drv->id_table, gio_dev) != NULL;
  97. }
  98. static int gio_device_probe(struct device *dev)
  99. {
  100. int error = -ENODEV;
  101. struct gio_driver *drv;
  102. struct gio_device *gio_dev;
  103. const struct gio_device_id *match;
  104. drv = to_gio_driver(dev->driver);
  105. gio_dev = to_gio_device(dev);
  106. if (!drv->probe)
  107. return error;
  108. gio_dev_get(gio_dev);
  109. match = gio_match_device(drv->id_table, gio_dev);
  110. if (match)
  111. error = drv->probe(gio_dev, match);
  112. if (error)
  113. gio_dev_put(gio_dev);
  114. return error;
  115. }
  116. static int gio_device_remove(struct device *dev)
  117. {
  118. struct gio_device *gio_dev = to_gio_device(dev);
  119. struct gio_driver *drv = to_gio_driver(dev->driver);
  120. if (dev->driver && drv->remove)
  121. drv->remove(gio_dev);
  122. return 0;
  123. }
  124. static int gio_device_suspend(struct device *dev, pm_message_t state)
  125. {
  126. struct gio_device *gio_dev = to_gio_device(dev);
  127. struct gio_driver *drv = to_gio_driver(dev->driver);
  128. int error = 0;
  129. if (dev->driver && drv->suspend)
  130. error = drv->suspend(gio_dev, state);
  131. return error;
  132. }
  133. static int gio_device_resume(struct device *dev)
  134. {
  135. struct gio_device *gio_dev = to_gio_device(dev);
  136. struct gio_driver *drv = to_gio_driver(dev->driver);
  137. int error = 0;
  138. if (dev->driver && drv->resume)
  139. error = drv->resume(gio_dev);
  140. return error;
  141. }
  142. static void gio_device_shutdown(struct device *dev)
  143. {
  144. struct gio_device *gio_dev = to_gio_device(dev);
  145. struct gio_driver *drv = to_gio_driver(dev->driver);
  146. if (dev->driver && drv->shutdown)
  147. drv->shutdown(gio_dev);
  148. }
  149. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  150. char *buf)
  151. {
  152. struct gio_device *gio_dev = to_gio_device(dev);
  153. int len = snprintf(buf, PAGE_SIZE, "gio:%x\n", gio_dev->id.id);
  154. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  155. }
  156. static ssize_t name_show(struct device *dev,
  157. struct device_attribute *attr, char *buf)
  158. {
  159. struct gio_device *giodev;
  160. giodev = to_gio_device(dev);
  161. return sprintf(buf, "%s", giodev->name);
  162. }
  163. static ssize_t id_show(struct device *dev,
  164. struct device_attribute *attr, char *buf)
  165. {
  166. struct gio_device *giodev;
  167. giodev = to_gio_device(dev);
  168. return sprintf(buf, "%x", giodev->id.id);
  169. }
  170. static struct device_attribute gio_dev_attrs[] = {
  171. __ATTR_RO(modalias),
  172. __ATTR_RO(name),
  173. __ATTR_RO(id),
  174. __ATTR_NULL,
  175. };
  176. static int gio_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  177. {
  178. struct gio_device *gio_dev = to_gio_device(dev);
  179. add_uevent_var(env, "MODALIAS=gio:%x", gio_dev->id.id);
  180. return 0;
  181. }
  182. int gio_register_driver(struct gio_driver *drv)
  183. {
  184. /* initialize common driver fields */
  185. if (!drv->driver.name)
  186. drv->driver.name = drv->name;
  187. if (!drv->driver.owner)
  188. drv->driver.owner = drv->owner;
  189. drv->driver.bus = &gio_bus_type;
  190. /* register with core */
  191. return driver_register(&drv->driver);
  192. }
  193. EXPORT_SYMBOL_GPL(gio_register_driver);
  194. void gio_unregister_driver(struct gio_driver *drv)
  195. {
  196. driver_unregister(&drv->driver);
  197. }
  198. EXPORT_SYMBOL_GPL(gio_unregister_driver);
  199. void gio_set_master(struct gio_device *dev)
  200. {
  201. u32 tmp = sgimc->giopar;
  202. switch (dev->slotno) {
  203. case 0:
  204. tmp |= SGIMC_GIOPAR_MASTERGFX;
  205. break;
  206. case 1:
  207. tmp |= SGIMC_GIOPAR_MASTEREXP0;
  208. break;
  209. case 2:
  210. tmp |= SGIMC_GIOPAR_MASTEREXP1;
  211. break;
  212. }
  213. sgimc->giopar = tmp;
  214. }
  215. EXPORT_SYMBOL_GPL(gio_set_master);
  216. void ip22_gio_set_64bit(int slotno)
  217. {
  218. u32 tmp = sgimc->giopar;
  219. switch (slotno) {
  220. case 0:
  221. tmp |= SGIMC_GIOPAR_GFX64;
  222. break;
  223. case 1:
  224. tmp |= SGIMC_GIOPAR_EXP064;
  225. break;
  226. case 2:
  227. tmp |= SGIMC_GIOPAR_EXP164;
  228. break;
  229. }
  230. sgimc->giopar = tmp;
  231. }
  232. static int ip22_gio_id(unsigned long addr, u32 *res)
  233. {
  234. u8 tmp8;
  235. u8 tmp16;
  236. u32 tmp32;
  237. u8 *ptr8;
  238. u16 *ptr16;
  239. u32 *ptr32;
  240. ptr32 = (void *)CKSEG1ADDR(addr);
  241. if (!get_dbe(tmp32, ptr32)) {
  242. /*
  243. * We got no DBE, but this doesn't mean anything.
  244. * If GIO is pipelined (which can't be disabled
  245. * for GFX slot) we don't get a DBE, but we see
  246. * the transfer size as data. So we do an 8bit
  247. * and a 16bit access and check whether the common
  248. * data matches
  249. */
  250. ptr8 = (void *)CKSEG1ADDR(addr + 3);
  251. if (get_dbe(tmp8, ptr8)) {
  252. /*
  253. * 32bit access worked, but 8bit doesn't
  254. * so we don't see phantom reads on
  255. * a pipelined bus, but a real card which
  256. * doesn't support 8 bit reads
  257. */
  258. *res = tmp32;
  259. return 1;
  260. }
  261. ptr16 = (void *)CKSEG1ADDR(addr + 2);
  262. get_dbe(tmp16, ptr16);
  263. if (tmp8 == (tmp16 & 0xff) &&
  264. tmp8 == (tmp32 & 0xff) &&
  265. tmp16 == (tmp32 & 0xffff)) {
  266. *res = tmp32;
  267. return 1;
  268. }
  269. }
  270. return 0; /* nothing here */
  271. }
  272. #define HQ2_MYSTERY_OFFS 0x6A07C
  273. #define NEWPORT_USTATUS_OFFS 0xF133C
  274. static int ip22_is_gr2(unsigned long addr)
  275. {
  276. u32 tmp;
  277. u32 *ptr;
  278. /* HQ2 only allows 32bit accesses */
  279. ptr = (void *)CKSEG1ADDR(addr + HQ2_MYSTERY_OFFS);
  280. if (!get_dbe(tmp, ptr)) {
  281. if (tmp == 0xdeadbeef)
  282. return 1;
  283. }
  284. return 0;
  285. }
  286. static void ip22_check_gio(int slotno, unsigned long addr, int irq)
  287. {
  288. const char *name = "Unknown";
  289. struct gio_device *gio_dev;
  290. u32 tmp;
  291. __u8 id;
  292. int i;
  293. /* first look for GR2/GR3 by checking mystery register */
  294. if (ip22_is_gr2(addr))
  295. tmp = 0x7f;
  296. else {
  297. if (!ip22_gio_id(addr, &tmp)) {
  298. /*
  299. * no GIO signature at start address of slot
  300. * since Newport doesn't have one, we check if
  301. * user status register is readable
  302. */
  303. if (ip22_gio_id(addr + NEWPORT_USTATUS_OFFS, &tmp))
  304. tmp = 0x7e;
  305. else
  306. tmp = 0;
  307. }
  308. }
  309. if (tmp) {
  310. id = GIO_ID(tmp);
  311. if (tmp & GIO_32BIT_ID) {
  312. if (tmp & GIO_64BIT_IFACE)
  313. ip22_gio_set_64bit(slotno);
  314. }
  315. for (i = 0; i < ARRAY_SIZE(gio_name_table); i++) {
  316. if (id == gio_name_table[i].id) {
  317. name = gio_name_table[i].name;
  318. break;
  319. }
  320. }
  321. printk(KERN_INFO "GIO: slot %d : %s (id %x)\n",
  322. slotno, name, id);
  323. gio_dev = kzalloc(sizeof *gio_dev, GFP_KERNEL);
  324. gio_dev->name = name;
  325. gio_dev->slotno = slotno;
  326. gio_dev->id.id = id;
  327. gio_dev->resource.start = addr;
  328. gio_dev->resource.end = addr + 0x3fffff;
  329. gio_dev->resource.flags = IORESOURCE_MEM;
  330. gio_dev->irq = irq;
  331. dev_set_name(&gio_dev->dev, "%d", slotno);
  332. gio_device_register(gio_dev);
  333. } else
  334. printk(KERN_INFO "GIO: slot %d : Empty\n", slotno);
  335. }
  336. static struct bus_type gio_bus_type = {
  337. .name = "gio",
  338. .dev_attrs = gio_dev_attrs,
  339. .match = gio_bus_match,
  340. .probe = gio_device_probe,
  341. .remove = gio_device_remove,
  342. .suspend = gio_device_suspend,
  343. .resume = gio_device_resume,
  344. .shutdown = gio_device_shutdown,
  345. .uevent = gio_device_uevent,
  346. };
  347. static struct resource gio_bus_resource = {
  348. .start = GIO_SLOT_GFX_BASE,
  349. .end = GIO_SLOT_GFX_BASE + 0x9fffff,
  350. .name = "GIO Bus",
  351. .flags = IORESOURCE_MEM,
  352. };
  353. int __init ip22_gio_init(void)
  354. {
  355. unsigned int pbdma __maybe_unused;
  356. int ret;
  357. ret = device_register(&gio_bus);
  358. if (ret)
  359. return ret;
  360. ret = bus_register(&gio_bus_type);
  361. if (!ret) {
  362. request_resource(&iomem_resource, &gio_bus_resource);
  363. printk(KERN_INFO "GIO: Probing bus...\n");
  364. if (ip22_is_fullhouse()) {
  365. /* Indigo2 */
  366. ip22_check_gio(0, GIO_SLOT_GFX_BASE, SGI_GIO_1_IRQ);
  367. ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIO_1_IRQ);
  368. } else {
  369. /* Indy/Challenge S */
  370. if (get_dbe(pbdma, (unsigned int *)&hpc3c1->pbdma[1]))
  371. ip22_check_gio(0, GIO_SLOT_GFX_BASE,
  372. SGI_GIO_0_IRQ);
  373. ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIOEXP0_IRQ);
  374. ip22_check_gio(2, GIO_SLOT_EXP1_BASE, SGI_GIOEXP1_IRQ);
  375. }
  376. } else
  377. device_unregister(&gio_bus);
  378. return ret;
  379. }
  380. subsys_initcall(ip22_gio_init);