sbus_drivers.txt 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. Writing SBUS Drivers
  2. David S. Miller (davem@redhat.com)
  3. The SBUS driver interfaces of the Linux kernel have been
  4. revamped completely for 2.4.x for several reasons. Foremost were
  5. performance and complexity concerns. This document details these
  6. new interfaces and how they are used to write an SBUS device driver.
  7. SBUS drivers need to include <asm/sbus.h> to get access
  8. to functions and structures described here.
  9. Probing and Detection
  10. Each SBUS device inside the machine is described by a
  11. structure called "struct sbus_dev". Likewise, each SBUS bus
  12. found in the system is described by a "struct sbus_bus". For
  13. each SBUS bus, the devices underneath are hung in a tree-like
  14. fashion off of the bus structure.
  15. The SBUS device structure contains enough information
  16. for you to implement your device probing algorithm and obtain
  17. the bits necessary to run your device. The most commonly
  18. used members of this structure, and their typical usage,
  19. will be detailed below.
  20. Here is a piece of skeleton code for performing a device
  21. probe in an SBUS driver under Linux:
  22. static int __devinit mydevice_probe_one(struct sbus_dev *sdev)
  23. {
  24. struct mysdevice *mp = kzalloc(sizeof(*mp), GFP_KERNEL);
  25. if (!mp)
  26. return -ENODEV;
  27. ...
  28. dev_set_drvdata(&sdev->ofdev.dev, mp);
  29. return 0;
  30. ...
  31. }
  32. static int __devinit mydevice_probe(struct of_device *dev,
  33. const struct of_device_id *match)
  34. {
  35. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  36. return mydevice_probe_one(sdev);
  37. }
  38. static int __devexit mydevice_remove(struct of_device *dev)
  39. {
  40. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  41. struct mydevice *mp = dev_get_drvdata(&dev->dev);
  42. return mydevice_remove_one(sdev, mp);
  43. }
  44. static struct of_device_id mydevice_match[] = {
  45. {
  46. .name = "mydevice",
  47. },
  48. {},
  49. };
  50. MODULE_DEVICE_TABLE(of, mydevice_match);
  51. static struct of_platform_driver mydevice_driver = {
  52. .name = "mydevice",
  53. .match_table = mydevice_match,
  54. .probe = mydevice_probe,
  55. .remove = __devexit_p(mydevice_remove),
  56. };
  57. static int __init mydevice_init(void)
  58. {
  59. return of_register_driver(&mydevice_driver, &sbus_bus_type);
  60. }
  61. static void __exit mydevice_exit(void)
  62. {
  63. of_unregister_driver(&mydevice_driver);
  64. }
  65. module_init(mydevice_init);
  66. module_exit(mydevice_exit);
  67. The mydevice_match table is a series of entries which
  68. describes what SBUS devices your driver is meant for. In the
  69. simplest case you specify a string for the 'name' field. Every
  70. SBUS device with a 'name' property matching your string will
  71. be passed one-by-one to your .probe method.
  72. You should store away your device private state structure
  73. pointer in the drvdata area so that you can retrieve it later on
  74. in your .remove method.
  75. Any memory allocated, registers mapped, IRQs registered,
  76. etc. must be undone by your .remove method so that all resources
  77. of your device are released by the time it returns.
  78. You should _NOT_ use the for_each_sbus(), for_each_sbusdev(),
  79. and for_all_sbusdev() interfaces. They are deprecated, will be
  80. removed, and no new driver should reference them ever.
  81. Mapping and Accessing I/O Registers
  82. Each SBUS device structure contains an array of descriptors
  83. which describe each register set. We abuse struct resource for that.
  84. They each correspond to the "reg" properties provided by the OBP firmware.
  85. Before you can access your device's registers you must map
  86. them. And later if you wish to shutdown your driver (for module
  87. unload or similar) you must unmap them. You must treat them as
  88. a resource, which you allocate (map) before using and free up
  89. (unmap) when you are done with it.
  90. The mapping information is stored in an opaque value
  91. typed as an "unsigned long". This is the type of the return value
  92. of the mapping interface, and the arguments to the unmapping
  93. interface. Let's say you want to map the first set of registers.
  94. Perhaps part of your driver software state structure looks like:
  95. struct mydevice {
  96. unsigned long control_regs;
  97. ...
  98. struct sbus_dev *sdev;
  99. ...
  100. };
  101. At initialization time you then use the sbus_ioremap
  102. interface to map in your registers, like so:
  103. static void init_one_mydevice(struct sbus_dev *sdev)
  104. {
  105. struct mydevice *mp;
  106. ...
  107. mp->control_regs = sbus_ioremap(&sdev->resource[0], 0,
  108. CONTROL_REGS_SIZE, "mydevice regs");
  109. if (!mp->control_regs) {
  110. /* Failure, cleanup and return. */
  111. }
  112. }
  113. Second argument to sbus_ioremap is an offset for
  114. cranky devices with broken OBP PROM. The sbus_ioremap uses only
  115. a start address and flags from the resource structure.
  116. Therefore it is possible to use the same resource to map
  117. several sets of registers or even to fabricate a resource
  118. structure if driver gets physical address from some private place.
  119. This practice is discouraged though. Use whatever OBP PROM
  120. provided to you.
  121. And here is how you might unmap these registers later at
  122. driver shutdown or module unload time, using the sbus_iounmap
  123. interface:
  124. static void mydevice_unmap_regs(struct mydevice *mp)
  125. {
  126. sbus_iounmap(mp->control_regs, CONTROL_REGS_SIZE);
  127. }
  128. Finally, to actually access your registers there are 6
  129. interface routines at your disposal. Accesses are byte (8 bit),
  130. word (16 bit), or longword (32 bit) sized. Here they are:
  131. u8 sbus_readb(unsigned long reg) /* read byte */
  132. u16 sbus_readw(unsigned long reg) /* read word */
  133. u32 sbus_readl(unsigned long reg) /* read longword */
  134. void sbus_writeb(u8 value, unsigned long reg) /* write byte */
  135. void sbus_writew(u16 value, unsigned long reg) /* write word */
  136. void sbus_writel(u32 value, unsigned long reg) /* write longword */
  137. So, let's say your device has a control register of some sort
  138. at offset zero. The following might implement resetting your device:
  139. #define CONTROL 0x00UL
  140. #define CONTROL_RESET 0x00000001 /* Reset hardware */
  141. static void mydevice_reset(struct mydevice *mp)
  142. {
  143. sbus_writel(CONTROL_RESET, mp->regs + CONTROL);
  144. }
  145. Or perhaps there is a data port register at an offset of
  146. 16 bytes which allows you to read bytes from a fifo in the device:
  147. #define DATA 0x10UL
  148. static u8 mydevice_get_byte(struct mydevice *mp)
  149. {
  150. return sbus_readb(mp->regs + DATA);
  151. }
  152. It's pretty straightforward, and clueful readers may have
  153. noticed that these interfaces mimick the PCI interfaces of the
  154. Linux kernel. This was not by accident.
  155. WARNING:
  156. DO NOT try to treat these opaque register mapping
  157. values as a memory mapped pointer to some structure
  158. which you can dereference.
  159. It may be memory mapped, it may not be. In fact it
  160. could be a physical address, or it could be the time
  161. of day xor'd with 0xdeadbeef. :-)
  162. Whatever it is, it's an implementation detail. The
  163. interface was done this way to shield the driver
  164. author from such complexities.
  165. Doing DVMA
  166. SBUS devices can perform DMA transactions in a way similar
  167. to PCI but dissimilar to ISA, e.g. DMA masters supply address.
  168. In contrast to PCI, however, that address (a bus address) is
  169. translated by IOMMU before a memory access is performed and therefore
  170. it is virtual. Sun calls this procedure DVMA.
  171. Linux supports two styles of using SBUS DVMA: "consistent memory"
  172. and "streaming DVMA". CPU view of consistent memory chunk is, well,
  173. consistent with a view of a device. Think of it as an uncached memory.
  174. Typically this way of doing DVMA is not very fast and drivers use it
  175. mostly for control blocks or queues. On some CPUs we cannot flush or
  176. invalidate individual pages or cache lines and doing explicit flushing
  177. over ever little byte in every control block would be wasteful.
  178. Streaming DVMA is a preferred way to transfer large amounts of data.
  179. This process works in the following way:
  180. 1. a CPU stops accessing a certain part of memory,
  181. flushes its caches covering that memory;
  182. 2. a device does DVMA accesses, then posts an interrupt;
  183. 3. CPU invalidates its caches and starts to access the memory.
  184. A single streaming DVMA operation can touch several discontiguous
  185. regions of a virtual bus address space. This is called a scatter-gather
  186. DVMA.
  187. [TBD: Why do not we neither Solaris attempt to map disjoint pages
  188. into a single virtual chunk with the help of IOMMU, so that non SG
  189. DVMA masters would do SG? It'd be very helpful for RAID.]
  190. In order to perform a consistent DVMA a driver does something
  191. like the following:
  192. char *mem; /* Address in the CPU space */
  193. u32 busa; /* Address in the SBus space */
  194. mem = (char *) sbus_alloc_consistent(sdev, MYMEMSIZE, &busa);
  195. Then mem is used when CPU accesses this memory and u32
  196. is fed to the device so that it can do DVMA. This is typically
  197. done with an sbus_writel() into some device register.
  198. Do not forget to free the DVMA resources once you are done:
  199. sbus_free_consistent(sdev, MYMEMSIZE, mem, busa);
  200. Streaming DVMA is more interesting. First you allocate some
  201. memory suitable for it or pin down some user pages. Then it all works
  202. like this:
  203. char *mem = argumen1;
  204. unsigned int size = argument2;
  205. u32 busa; /* Address in the SBus space */
  206. *mem = 1; /* CPU can access */
  207. busa = sbus_map_single(sdev, mem, size);
  208. if (busa == 0) .......
  209. /* Tell the device to use busa here */
  210. /* CPU cannot access the memory without sbus_dma_sync_single() */
  211. sbus_unmap_single(sdev, busa, size);
  212. if (*mem == 0) .... /* CPU can access again */
  213. It is possible to retain mappings and ask the device to
  214. access data again and again without calling sbus_unmap_single.
  215. However, CPU caches must be invalidated with sbus_dma_sync_single
  216. before such access.
  217. [TBD but what about writeback caches here... do we have any?]
  218. There is an equivalent set of functions doing the same thing
  219. only with several memory segments at once for devices capable of
  220. scatter-gather transfers. Use the Source, Luke.
  221. Examples
  222. drivers/net/sunhme.c
  223. This is a complicated driver which illustrates many concepts
  224. discussed above and plus it handles both PCI and SBUS boards.
  225. drivers/scsi/esp.c
  226. Check it out for scatter-gather DVMA.
  227. drivers/sbus/char/bpp.c
  228. A non-DVMA device.
  229. drivers/net/sunlance.c
  230. Lance driver abuses consistent mappings for data transfer.
  231. It is a nifty trick which we do not particularly recommend...
  232. Just check it out and know that it's legal.