ip22-gio.c 9.4 KB

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