ip22-gio.c 9.4 KB

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