1 FPGA Manager 1 FPGA Manager 2 ============ 2 ============ 3 3 4 Overview 4 Overview 5 -------- 5 -------- 6 6 7 The FPGA manager core exports a set of functio 7 The FPGA manager core exports a set of functions for programming an FPGA with 8 an image. The API is manufacturer agnostic. 8 an image. The API is manufacturer agnostic. All manufacturer specifics are 9 hidden away in a low level driver which regist 9 hidden away in a low level driver which registers a set of ops with the core. 10 The FPGA image data itself is very manufacture 10 The FPGA image data itself is very manufacturer specific, but for our purposes 11 it's just binary data. The FPGA manager core 11 it's just binary data. The FPGA manager core won't parse it. 12 12 13 The FPGA image to be programmed can be in a sc 13 The FPGA image to be programmed can be in a scatter gather list, a single 14 contiguous buffer, or a firmware file. Becaus 14 contiguous buffer, or a firmware file. Because allocating contiguous kernel 15 memory for the buffer should be avoided, users 15 memory for the buffer should be avoided, users are encouraged to use a scatter 16 gather list instead if possible. 16 gather list instead if possible. 17 17 18 The particulars for programming the image are 18 The particulars for programming the image are presented in a structure (struct 19 fpga_image_info). This struct contains parame 19 fpga_image_info). This struct contains parameters such as pointers to the 20 FPGA image as well as image-specific particula 20 FPGA image as well as image-specific particulars such as whether the image was 21 built for full or partial reconfiguration. 21 built for full or partial reconfiguration. 22 22 23 How to support a new FPGA device 23 How to support a new FPGA device 24 -------------------------------- 24 -------------------------------- 25 25 26 To add another FPGA manager, write a driver th 26 To add another FPGA manager, write a driver that implements a set of ops. The 27 probe function calls ``fpga_mgr_register()`` o !! 27 probe function calls fpga_mgr_register(), such as:: 28 such as:: << 29 28 30 static const struct fpga_manager_ops s 29 static const struct fpga_manager_ops socfpga_fpga_ops = { 31 .write_init = socfpga_fpga_ops 30 .write_init = socfpga_fpga_ops_configure_init, 32 .write = socfpga_fpga_ops_conf 31 .write = socfpga_fpga_ops_configure_write, 33 .write_complete = socfpga_fpga 32 .write_complete = socfpga_fpga_ops_configure_complete, 34 .state = socfpga_fpga_ops_stat 33 .state = socfpga_fpga_ops_state, 35 }; 34 }; 36 35 37 static int socfpga_fpga_probe(struct p 36 static int socfpga_fpga_probe(struct platform_device *pdev) 38 { 37 { 39 struct device *dev = &pdev->de 38 struct device *dev = &pdev->dev; 40 struct socfpga_fpga_priv *priv 39 struct socfpga_fpga_priv *priv; 41 struct fpga_manager *mgr; 40 struct fpga_manager *mgr; 42 int ret; 41 int ret; 43 42 44 priv = devm_kzalloc(dev, sizeo 43 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 45 if (!priv) 44 if (!priv) 46 return -ENOMEM; 45 return -ENOMEM; 47 46 48 /* 47 /* 49 * do ioremaps, get interrupts 48 * do ioremaps, get interrupts, etc. and save 50 * them in priv 49 * them in priv 51 */ 50 */ 52 51 53 mgr = fpga_mgr_register(dev, " !! 52 mgr = fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager", 54 &socfp !! 53 &socfpga_fpga_ops, priv); 55 if (IS_ERR(mgr)) !! 54 if (!mgr) 56 return PTR_ERR(mgr); !! 55 return -ENOMEM; 57 56 58 platform_set_drvdata(pdev, mgr 57 platform_set_drvdata(pdev, mgr); 59 58 60 return 0; !! 59 ret = fpga_mgr_register(mgr); >> 60 if (ret) >> 61 fpga_mgr_free(mgr); >> 62 >> 63 return ret; 61 } 64 } 62 65 63 static int socfpga_fpga_remove(struct 66 static int socfpga_fpga_remove(struct platform_device *pdev) 64 { 67 { 65 struct fpga_manager *mgr = pla 68 struct fpga_manager *mgr = platform_get_drvdata(pdev); 66 69 67 fpga_mgr_unregister(mgr); 70 fpga_mgr_unregister(mgr); 68 71 69 return 0; 72 return 0; 70 } 73 } 71 74 72 Alternatively, the probe function could call o << 73 register functions, ``devm_fpga_mgr_register() << 74 ``devm_fpga_mgr_register_full()``. When these << 75 parameter syntax is the same, but the call to << 76 removed. In the above example, the ``socfpga_f << 77 required. << 78 75 79 The ops will implement whatever device specifi 76 The ops will implement whatever device specific register writes are needed to 80 do the programming sequence for this particula 77 do the programming sequence for this particular FPGA. These ops return 0 for 81 success or negative error codes otherwise. 78 success or negative error codes otherwise. 82 79 83 The programming sequence is:: 80 The programming sequence is:: 84 1. .parse_header (optional, may be called onc !! 81 1. .write_init 85 2. .write_init !! 82 2. .write or .write_sg (may be called once or multiple times) 86 3. .write or .write_sg (may be called once or !! 83 3. .write_complete 87 4. .write_complete << 88 << 89 The .parse_header function will set header_siz << 90 struct fpga_image_info. Before parse_header ca << 91 with initial_header_size. If flag skip_header << 92 .write function will get image buffer starting << 93 beginning. If data_size is set, .write functio << 94 the image buffer, otherwise .write will get da << 95 This will not affect .write_sg, .write_sg will << 96 sg_table form. If FPGA image is already mapped << 97 whole buffer will be passed into .parse_header << 98 form, core code will buffer up at least .initi << 99 call of .parse_header, if it is not enough, .p << 100 size into info->header_size and return -EAGAIN << 101 with greater part of image buffer on the input << 102 84 103 The .write_init function will prepare the FPGA !! 85 The .write_init function will prepare the FPGA to receive the image data. The 104 buffer passed into .write_init will be at leas !! 86 buffer passed into .write_init will be atmost .initial_header_size bytes long, 105 if the whole bitstream is not immediately avai 87 if the whole bitstream is not immediately available then the core code will 106 buffer up at least this much before starting. 88 buffer up at least this much before starting. 107 89 108 The .write function writes a buffer to the FPG 90 The .write function writes a buffer to the FPGA. The buffer may be contain the 109 whole FPGA image or may be a smaller chunk of 91 whole FPGA image or may be a smaller chunk of an FPGA image. In the latter 110 case, this function is called multiple times f 92 case, this function is called multiple times for successive chunks. This interface 111 is suitable for drivers which use PIO. 93 is suitable for drivers which use PIO. 112 94 113 The .write_sg version behaves the same as .wri 95 The .write_sg version behaves the same as .write except the input is a sg_table 114 scatter list. This interface is suitable for d 96 scatter list. This interface is suitable for drivers which use DMA. 115 97 116 The .write_complete function is called after a 98 The .write_complete function is called after all the image has been written 117 to put the FPGA into operating mode. 99 to put the FPGA into operating mode. 118 100 119 The ops include a .state function which will d !! 101 The ops include a .state function which will read the hardware FPGA manager and 120 and return a code of type enum fpga_mgr_states !! 102 return a code of type enum fpga_mgr_states. It doesn't result in a change in 121 in state. !! 103 hardware state. 122 104 123 API for implementing a new FPGA Manager driver !! 105 How to write an image buffer to a supported FPGA 124 ---------------------------------------------- !! 106 ------------------------------------------------ 125 107 126 * ``fpga_mgr_states`` - Values for :c:expr:`f !! 108 Some sample code:: 127 * struct fpga_manager - the FPGA manager stru << 128 * struct fpga_manager_ops - Low level FPGA ma << 129 * struct fpga_manager_info - Parameter struct << 130 * __fpga_mgr_register_full() - Create and reg << 131 fpga_mgr_info structure to provide the full << 132 * __fpga_mgr_register() - Create and register << 133 arguments << 134 * __devm_fpga_mgr_register_full() - Resource << 135 __fpga_mgr_register_full() << 136 * __devm_fpga_mgr_register() - Resource manag << 137 * fpga_mgr_unregister() - Unregister an FPGA << 138 << 139 Helper macros ``fpga_mgr_register_full()``, `` << 140 ``devm_fpga_mgr_register_full()``, and ``devm_ << 141 to ease the registration. << 142 109 143 .. kernel-doc:: include/linux/fpga/fpga-mgr.h !! 110 #include <linux/fpga/fpga-mgr.h> 144 :functions: fpga_mgr_states !! 111 >> 112 struct fpga_manager *mgr; >> 113 struct fpga_image_info *info; >> 114 int ret; >> 115 >> 116 /* >> 117 * Get a reference to FPGA manager. The manager is not locked, so you can >> 118 * hold onto this reference without it preventing programming. >> 119 * >> 120 * This example uses the device node of the manager. Alternatively, use >> 121 * fpga_mgr_get(dev) instead if you have the device. >> 122 */ >> 123 mgr = of_fpga_mgr_get(mgr_node); >> 124 >> 125 /* struct with information about the FPGA image to program. */ >> 126 info = fpga_image_info_alloc(dev); >> 127 >> 128 /* flags indicates whether to do full or partial reconfiguration */ >> 129 info->flags = FPGA_MGR_PARTIAL_RECONFIG; >> 130 >> 131 /* >> 132 * At this point, indicate where the image is. This is pseudo-code; you're >> 133 * going to use one of these three. >> 134 */ >> 135 if (image is in a scatter gather table) { >> 136 >> 137 info->sgt = [your scatter gather table] >> 138 >> 139 } else if (image is in a buffer) { >> 140 >> 141 info->buf = [your image buffer] >> 142 info->count = [image buffer size] >> 143 >> 144 } else if (image is in a firmware file) { >> 145 >> 146 info->firmware_name = devm_kstrdup(dev, firmware_name, GFP_KERNEL); >> 147 >> 148 } >> 149 >> 150 /* Get exclusive control of FPGA manager */ >> 151 ret = fpga_mgr_lock(mgr); >> 152 >> 153 /* Load the buffer to the FPGA */ >> 154 ret = fpga_mgr_buf_load(mgr, &info, buf, count); >> 155 >> 156 /* Release the FPGA manager */ >> 157 fpga_mgr_unlock(mgr); >> 158 fpga_mgr_put(mgr); >> 159 >> 160 /* Deallocate the image info if you're done with it */ >> 161 fpga_image_info_free(info); >> 162 >> 163 API for implementing a new FPGA Manager driver >> 164 ---------------------------------------------- 145 165 146 .. kernel-doc:: include/linux/fpga/fpga-mgr.h 166 .. kernel-doc:: include/linux/fpga/fpga-mgr.h 147 :functions: fpga_manager 167 :functions: fpga_manager 148 168 149 .. kernel-doc:: include/linux/fpga/fpga-mgr.h 169 .. kernel-doc:: include/linux/fpga/fpga-mgr.h 150 :functions: fpga_manager_ops 170 :functions: fpga_manager_ops 151 171 >> 172 .. kernel-doc:: drivers/fpga/fpga-mgr.c >> 173 :functions: fpga_mgr_create >> 174 >> 175 .. kernel-doc:: drivers/fpga/fpga-mgr.c >> 176 :functions: fpga_mgr_free >> 177 >> 178 .. kernel-doc:: drivers/fpga/fpga-mgr.c >> 179 :functions: fpga_mgr_register >> 180 >> 181 .. kernel-doc:: drivers/fpga/fpga-mgr.c >> 182 :functions: fpga_mgr_unregister >> 183 >> 184 API for programming a FPGA >> 185 -------------------------- >> 186 152 .. kernel-doc:: include/linux/fpga/fpga-mgr.h 187 .. kernel-doc:: include/linux/fpga/fpga-mgr.h 153 :functions: fpga_manager_info !! 188 :functions: fpga_image_info >> 189 >> 190 .. kernel-doc:: include/linux/fpga/fpga-mgr.h >> 191 :functions: fpga_mgr_states 154 192 155 .. kernel-doc:: drivers/fpga/fpga-mgr.c 193 .. kernel-doc:: drivers/fpga/fpga-mgr.c 156 :functions: __fpga_mgr_register_full !! 194 :functions: fpga_image_info_alloc 157 195 158 .. kernel-doc:: drivers/fpga/fpga-mgr.c 196 .. kernel-doc:: drivers/fpga/fpga-mgr.c 159 :functions: __fpga_mgr_register !! 197 :functions: fpga_image_info_free 160 198 161 .. kernel-doc:: drivers/fpga/fpga-mgr.c 199 .. kernel-doc:: drivers/fpga/fpga-mgr.c 162 :functions: __devm_fpga_mgr_register_full !! 200 :functions: of_fpga_mgr_get 163 201 164 .. kernel-doc:: drivers/fpga/fpga-mgr.c 202 .. kernel-doc:: drivers/fpga/fpga-mgr.c 165 :functions: __devm_fpga_mgr_register !! 203 :functions: fpga_mgr_get 166 204 167 .. kernel-doc:: drivers/fpga/fpga-mgr.c 205 .. kernel-doc:: drivers/fpga/fpga-mgr.c 168 :functions: fpga_mgr_unregister !! 206 :functions: fpga_mgr_put >> 207 >> 208 .. kernel-doc:: drivers/fpga/fpga-mgr.c >> 209 :functions: fpga_mgr_lock >> 210 >> 211 .. kernel-doc:: drivers/fpga/fpga-mgr.c >> 212 :functions: fpga_mgr_unlock >> 213 >> 214 .. kernel-doc:: include/linux/fpga/fpga-mgr.h >> 215 :functions: fpga_mgr_states >> 216 >> 217 Note - use :c:func:`fpga_region_program_fpga()` instead of :c:func:`fpga_mgr_load()` >> 218 >> 219 .. kernel-doc:: drivers/fpga/fpga-mgr.c >> 220 :functions: fpga_mgr_load
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.