1 /* 2 * cb710/cb710.h 3 * 4 * Copyright by Michał Mirosław, 2008-2009 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 #ifndef LINUX_CB710_DRIVER_H 11 #define LINUX_CB710_DRIVER_H 12 13 /* verify assumptions on platform_device framework */ 14 #define CONFIG_CB710_DEBUG_ASSUMPTIONS 15 16 #include <linux/io.h> 17 #include <linux/interrupt.h> 18 #include <linux/spinlock.h> 19 #include <linux/pci.h> 20 #include <linux/platform_device.h> 21 #include <linux/mmc/host.h> 22 23 struct cb710_slot; 24 25 typedef int (*cb710_irq_handler_t)(struct cb710_slot *); 26 27 /* per-virtual-slot structure */ 28 struct cb710_slot { 29 struct platform_device pdev; 30 void __iomem *iobase; 31 cb710_irq_handler_t irq_handler; 32 }; 33 34 /* per-device structure */ 35 struct cb710_chip { 36 struct pci_dev *pdev; 37 void __iomem *iobase; 38 unsigned platform_id; 39 #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS 40 atomic_t slot_refs_count; 41 #endif 42 unsigned slot_mask; 43 unsigned slots; 44 spinlock_t irq_lock; 45 struct cb710_slot slot[0]; 46 }; 47 48 /* NOTE: cb710_chip.slots is modified only during device init/exit and 49 * they are all serialized wrt themselves */ 50 51 /* cb710_chip.slot_mask values */ 52 #define CB710_SLOT_MMC 1 53 #define CB710_SLOT_MS 2 54 #define CB710_SLOT_SM 4 55 56 /* slot port accessors - so the logic is more clear in the code */ 57 #define CB710_PORT_ACCESSORS(t) \ 58 static inline void cb710_write_port_##t(struct cb710_slot *slot, \ 59 unsigned port, u##t value) \ 60 { \ 61 iowrite##t(value, slot->iobase + port); \ 62 } \ 63 \ 64 static inline u##t cb710_read_port_##t(struct cb710_slot *slot, \ 65 unsigned port) \ 66 { \ 67 return ioread##t(slot->iobase + port); \ 68 } \ 69 \ 70 static inline void cb710_modify_port_##t(struct cb710_slot *slot, \ 71 unsigned port, u##t set, u##t clear) \ 72 { \ 73 iowrite##t( \ 74 (ioread##t(slot->iobase + port) & ~clear)|set, \ 75 slot->iobase + port); \ 76 } 77 78 CB710_PORT_ACCESSORS(8) 79 CB710_PORT_ACCESSORS(16) 80 CB710_PORT_ACCESSORS(32) 81 82 void cb710_pci_update_config_reg(struct pci_dev *pdev, 83 int reg, uint32_t and, uint32_t xor); 84 void cb710_set_irq_handler(struct cb710_slot *slot, 85 cb710_irq_handler_t handler); 86 87 /* some device struct walking */ 88 89 static inline struct cb710_slot *cb710_pdev_to_slot( 90 struct platform_device *pdev) 91 { 92 return container_of(pdev, struct cb710_slot, pdev); 93 } 94 95 static inline struct cb710_chip *cb710_slot_to_chip(struct cb710_slot *slot) 96 { 97 return dev_get_drvdata(slot->pdev.dev.parent); 98 } 99 100 static inline struct device *cb710_slot_dev(struct cb710_slot *slot) 101 { 102 return &slot->pdev.dev; 103 } 104 105 static inline struct device *cb710_chip_dev(struct cb710_chip *chip) 106 { 107 return &chip->pdev->dev; 108 } 109 110 /* debugging aids */ 111 112 #ifdef CONFIG_CB710_DEBUG 113 void cb710_dump_regs(struct cb710_chip *chip, unsigned dump); 114 #else 115 #define cb710_dump_regs(c, d) do {} while (0) 116 #endif 117 118 #define CB710_DUMP_REGS_MMC 0x0F 119 #define CB710_DUMP_REGS_MS 0x30 120 #define CB710_DUMP_REGS_SM 0xC0 121 #define CB710_DUMP_REGS_ALL 0xFF 122 #define CB710_DUMP_REGS_MASK 0xFF 123 124 #define CB710_DUMP_ACCESS_8 0x100 125 #define CB710_DUMP_ACCESS_16 0x200 126 #define CB710_DUMP_ACCESS_32 0x400 127 #define CB710_DUMP_ACCESS_ALL 0x700 128 #define CB710_DUMP_ACCESS_MASK 0x700 129 130 #endif /* LINUX_CB710_DRIVER_H */ 131 /* 132 * cb710/sgbuf2.h 133 * 134 * Copyright by Michał Mirosław, 2008-2009 135 * 136 * This program is free software; you can redistribute it and/or modify 137 * it under the terms of the GNU General Public License version 2 as 138 * published by the Free Software Foundation. 139 */ 140 #ifndef LINUX_CB710_SG_H 141 #define LINUX_CB710_SG_H 142 143 #include <linux/highmem.h> 144 #include <linux/scatterlist.h> 145 146 /** 147 * cb710_sg_miter_stop_writing - stop mapping iteration after writing 148 * @miter: sg mapping iter to be stopped 149 * 150 * Description: 151 * Stops mapping iterator @miter. @miter should have been started 152 * started using sg_miter_start(). A stopped iteration can be 153 * resumed by calling sg_miter_next() on it. This is useful when 154 * resources (kmap) need to be released during iteration. 155 * 156 * This is a convenience wrapper that will be optimized out for arches 157 * that don't need flush_kernel_dcache_page(). 158 * 159 * Context: 160 * IRQ disabled if the SG_MITER_ATOMIC is set. Don't care otherwise. 161 */ 162 static inline void cb710_sg_miter_stop_writing(struct sg_mapping_iter *miter) 163 { 164 if (miter->page) 165 flush_kernel_dcache_page(miter->page); 166 sg_miter_stop(miter); 167 } 168 169 /* 170 * 32-bit PIO mapping sg iterator 171 * 172 * Hides scatterlist access issues - fragment boundaries, alignment, page 173 * mapping - for drivers using 32-bit-word-at-a-time-PIO (ie. PCI devices 174 * without DMA support). 175 * 176 * Best-case reading (transfer from device): 177 * sg_miter_start(); 178 * cb710_sg_dwiter_write_from_io(); 179 * cb710_sg_miter_stop_writing(); 180 * 181 * Best-case writing (transfer to device): 182 * sg_miter_start(); 183 * cb710_sg_dwiter_read_to_io(); 184 * sg_miter_stop(); 185 */ 186 187 uint32_t cb710_sg_dwiter_read_next_block(struct sg_mapping_iter *miter); 188 void cb710_sg_dwiter_write_next_block(struct sg_mapping_iter *miter, uint32_t data); 189 190 /** 191 * cb710_sg_dwiter_write_from_io - transfer data to mapped buffer from 32-bit IO port 192 * @miter: sg mapping iter 193 * @port: PIO port - IO or MMIO address 194 * @count: number of 32-bit words to transfer 195 * 196 * Description: 197 * Reads @count 32-bit words from register @port and stores it in 198 * buffer iterated by @miter. Data that would overflow the buffer 199 * is silently ignored. Iterator is advanced by 4*@count bytes 200 * or to the buffer's end whichever is closer. 201 * 202 * Context: 203 * IRQ disabled if the SG_MITER_ATOMIC is set. Don't care otherwise. 204 */ 205 static inline void cb710_sg_dwiter_write_from_io(struct sg_mapping_iter *miter, 206 void __iomem *port, size_t count) 207 { 208 while (count-- > 0) 209 cb710_sg_dwiter_write_next_block(miter, ioread32(port)); 210 } 211 212 /** 213 * cb710_sg_dwiter_read_to_io - transfer data to 32-bit IO port from mapped buffer 214 * @miter: sg mapping iter 215 * @port: PIO port - IO or MMIO address 216 * @count: number of 32-bit words to transfer 217 * 218 * Description: 219 * Writes @count 32-bit words to register @port from buffer iterated 220 * through @miter. If buffer ends before @count words are written 221 * missing data is replaced by zeroes. @miter is advanced by 4*@count 222 * bytes or to the buffer's end whichever is closer. 223 * 224 * Context: 225 * IRQ disabled if the SG_MITER_ATOMIC is set. Don't care otherwise. 226 */ 227 static inline void cb710_sg_dwiter_read_to_io(struct sg_mapping_iter *miter, 228 void __iomem *port, size_t count) 229 { 230 while (count-- > 0) 231 iowrite32(cb710_sg_dwiter_read_next_block(miter), port); 232 } 233 234 #endif /* LINUX_CB710_SG_H */ 235